0

I'm trying to serialize List to json file like this

class ScientificDoubleConverter : JsonConverter
{
  public override bool CanRead { get { return true; } }
  public override bool CanConvert(Type objectType)
  {
    return true;
  }

  public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
  {
    if (value is List<double> myList)
    {

      JArray myArray = new JArray();
      myList.ForEach(dblValue => myArray.Add(dblValue.ToString("E")));
      myArray.WriteTo(writer);

    }
  }
}

This works, but i get values with qoutes

"values": ["-9,811880E-002", "2,236657E-002", "-4,020144E-001", ...

I would like no quotes and a point in value scientific notation

"values": [-9.811880E-002, 2.236657E-002, -4.020144E-001, ...
Juri
  • 1,531
  • 2
  • 20
  • 43

3 Answers3

4

You're explicitly formatting the values. Don't do that - add the elements directly to the array:

if (value is List<double> myList)
{
    JArray myArray = new JArray();
    foreach (var element in myList)
    {
        myArray.Add(element);
    }
    myArray.WriteTo(writer);
}

Or write directly to the writer:

if (value is List<double> myList)
{
    writer.WriteStartArray();
    foreach (var element in myList)
    {
        writer.WriteValue(element);
    }
    writer.WriteEndArray();
}

This may not get to the exact representation you want (in terms of scientific notation), but it will get to a valid JSON representation of the data you're trying to serialize. Anything reading the JSON should be able to read the exact same value from the JSON.

If you absolutely have to customize the format, you can use JsonWriter.WriteRawValue, using the invariant culture to format your values. But I'd strongly advise you not to. I'd be really surprised at a JSON parser that can't handle the regular output of Json.NET. Here's a complete example if you really, really have to do it:

using System;
using System.Globalization;
using System.Collections.Generic;
using Newtonsoft.Json;

class Program
{    
    static void Main(string[] args)
    {
        var writer = new JsonTextWriter(Console.Out);
        var list = new List<double>
        {
            -9.811880E-002,
            2.236657E-002,
            -4.020144E-001
        };
        WriteList(writer, list);
    }

    static void WriteList(JsonWriter writer, List<double> list)
    {
        writer.WriteStartArray();
        foreach (var element in list)
        {
            writer.WriteRawValue(element.ToString("E", CultureInfo.InvariantCulture));
        }
        writer.WriteEndArray();
    }
}

Output:

[-9.811880E-002,2.236657E-002,-4.020144E-001]
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • this wil not give me the scientific notation – Juri Nov 14 '19 at 15:18
  • @Juri: No, but it will give you valid JSON which can be read by any JSON parser to give the right result. What is your reason for needing scientific notation? – Jon Skeet Nov 14 '19 at 15:22
  • @Juri: I'll see whether I can write some code to do that, but I would *strongly* suggest that you don't use it. The more "customized" your serialization is, the more likely it is you'll end up with a bug in it. – Jon Skeet Nov 14 '19 at 15:24
  • data export for MathLab – Juri Nov 14 '19 at 15:24
  • 2
    @Juri: Are you saying MathLab can't parse the standard JSON created by Json.NET? That would seem highly odd. – Jon Skeet Nov 14 '19 at 15:27
  • 1
    @Juri: I've added a way of serializing to scientific notation, but as I say, I'd be really surprised if you actually needed it. – Jon Skeet Nov 14 '19 at 15:29
0

You're doing dblValue.ToString("E") which is why you're getting strings.

Soham Dasgupta
  • 5,061
  • 24
  • 79
  • 125
0

JArray.Add except Object too as argument. So, you don't have to convert your double to string. Just directly add to array.

Have a look here for details.

nzrytmn
  • 6,193
  • 1
  • 41
  • 38