-2

It is necessary to write value of "Coordinates" property in json without hyphenation for the following lines, not using ToString() (without converting the value to a string). The desired result is shown below.

{
          "Id": null,
          "Style": "1234",
          "Geometry": {
            "Type": "Polygon",
            "Coordinates": [[[47541.470259278358,6846.8710054924586],[47540.359922950891,6845.4552435801925],[47541.470259278358,6846.8710054924586]]],
            "Properties": [
              {
                "PointType": "Straight"
              },
              {
                "PointType": "Straight"
              },
              {
                "PointType": "Straight"
              }
            ]
       }
}

but not:

{
          "Id": null,
          "Style": "1234",
          "Geometry": {
            "Type": "Polygon",
            "Coordinates": "[[[47541.470259278358,6846.8710054924586],[47540.359922950891,6845.4552435801925],[47541.470259278358,6846.8710054924586]]]",
            "Properties": [
              {
                "PointType": "Straight"
              },
              {
                "PointType": "Straight"
              },
              {
                "PointType": "Straight"
              }
            ]
       }
}

A function that serializes the corresponding class object in json:

JToken ToJson()
        {
            using (var writer = new JTokenWriter()) {
                JsonSerializer serializer = new JsonSerializer();
                serializer.Serialize(writer, this);
                return writer.Token;
            }
        }
DonilZ
  • 5
  • 5
  • By "one line", do you mean one line of C# code or one line of JSON? – Sweeper Mar 28 '19 at 10:10
  • one line of JSON – DonilZ Mar 28 '19 at 10:13
  • Please provide us some line of codes and tell us which parser do you use for the serialization. – Kinimod Mar 28 '19 at 10:13
  • Whitespace in JSON does not matter, so whether the nested array is printed on one line or multiple lines, from the point of view of the parser/writer, the JSON is still the same JSON. Just write the JSON normally. You just need a custom way of printing your JSON out. – Sweeper Mar 28 '19 at 10:16
  • By default, the json is displayed with the line-break of each sub-array and cells to a new one on the following lines. Is it possible to remove hyphens for a particular property ("Coordinates")? – DonilZ Mar 28 '19 at 10:31

1 Answers1

0

it seems your second case contains Coordinates property as serialized string.

why should not use var string = JsonConvert.SerializeObject(YourObject) ?

But you should install https://www.nuget.org/packages/Newtonsoft.Json/ first

You could use string type for properties where you need double quotes and use array or number if you don't need it

Oleg Bondarenko
  • 1,694
  • 1
  • 16
  • 19
  • Exactly. It is not good for me. To write the value of the property in one line, it is suggested to convert the value into a string (as here https://stackoverflow.com/questions/570689/how-to-serialize-a-jobject-without-the-formatting/572076). But I need to achieve a result without converting value of property to a string. – DonilZ Mar 28 '19 at 10:39
  • How is your Coordinates property presented in c# class ? List>> ? – Oleg Bondarenko Mar 28 '19 at 11:01
  • Yes, List>> – DonilZ Mar 28 '19 at 11:28
  • JsonConvert.SerializeObject(YourObject) returns desired result, or I am something missed ? – Oleg Bondarenko Mar 28 '19 at 11:30
  • So he write json at all without line-breaks. I will try in this case to add line-breaks everywhere except "Coordinates". – DonilZ Mar 28 '19 at 11:44
  • there is my result string "{"id":null,"data":[[[10.0,11.0]]]}" without line-breaks. – Oleg Bondarenko Mar 28 '19 at 11:51
  • Ah I guess you meant double quotes instead line-breaks. For string properties it is always value in double quotes in json string result. For number types for example it is without quotes at all and the same for arrays. – Oleg Bondarenko Mar 28 '19 at 12:09
  • I want to ensure that there are no quotes in the "Coordinates" property value (because it is an array) and there are no line-breaks between subarrays. For example: "Coordinates" : [[[1,2],[3,4]],[[5,6],[7,8]]]. But by default json separates subarrays with line-breaks. This can be fixed by converting the value of Coordinates property to a string, BUT is it possible to fix it in another way without converting to a string? – DonilZ Mar 28 '19 at 12:32
  • I have not seen any line -breaks on json string "{"Test":null,"Data":[[[10.0,11.0],[14.0,17.0]]]}". But ok you could convert Coordinates array to string and after that deserialize main object and manually deserialize Coordinates property too. But Could you explain me how you got line-breaks between sub-arrays ? – Oleg Bondarenko Mar 28 '19 at 12:38
  • When I wrote an object serialized in json: Console.WriteLine(obj.ToJson()); (the ToJson() function code is given in the question). All properties and sub-properties are automatically separated by line-breaks. But I understand you, I will try to do as you said, thanks. – DonilZ Mar 28 '19 at 12:52
  • I got it you are using different JsonSerializer, could you use Newtonsoft.Json instead ? it works without redundant line-breaks and other stuff. Also you could try to use Console.WriteLine(obj.ToJson().toString()) – Oleg Bondarenko Mar 28 '19 at 12:55
  • Because your application console shows not string but JToken value – Oleg Bondarenko Mar 28 '19 at 13:01