1

Beforehand; I'm using an old version of Newtonsoft.Json (4.0.8.0).

So I'm trying to write a .NET client for a webserver application. To convert all incoming packets from a json structure to .NET object i do use the JSON Serializer with the inbuilt function JToken.ToObject. This requires the target .net class to have the needed attributes named exactly as the incoming json data ones.

Now I came across a data packet which contains invalid property names in the scope of C# (.NET overall I think). It looks like this.

"12345" : {
  "Name1/Part2": {}
  "Name2/Part2": {}
  "Name3/Part2": {}
  "Name4/Part2": {}
  "Name5/Part2": {}
}

the equal .net code would be.

class DataPacket {
  public DummyObject 12345 {get; set;}


  public class DummyObject {
    public object Name1/Part2 {get; set}
    public object Name2/Part2 {get; set}
    public object Name3/Part2 {get; set}
    public object Name4/Part2 {get; set}
    public object Name5/Part2 {get; set}
  }
}

where as all property names are illegal ('starting with number', 'illegal characters -> /').

Any idea how I can solve this problem with the Major 4 Version of Newtonsoft?

Many thanks

StealthRT
  • 10,108
  • 40
  • 183
  • 342
user3411789
  • 45
  • 1
  • 6

1 Answers1

1

I believe you can use the jsonproperty attribute so for the first one something like:

class DataPacket{
                    [JsonProperty(PropertyName="1234")]
                    public DummyObject OneTwoThreeFour {get;set;}//or whatever you want to name.
                }

do that for each json property you need to convert that has illegal names

S.Will
  • 380
  • 4
  • 13