Click here to Skip to main content
15,868,349 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all. I'm kind of a newbie using JSON serialization, so any help would be greatly appreciated! Plus, I'm at my wits end. I'm using the standard Newtonsoft JSON serializer and I need to somehow get my directoryIds property to serialize exactly like result #2 below. Currently, I'm getting result #1. I checked to make sure both were valid JSON formats using this Web validator : http://jsonformatter.curiousconcept.com/[^]. I've only posted a partial JSON object for reference.
C#
...
[DataMember]
public DirectoryId[] directoryIds { get; set; }
...

[DataContract]
public class DirectoryId
{
    [DataMember]
    [JsonProperty(PropertyName = "string")]
    public string Str { get; set; }
}
Here's the code to serialize the object just in case it's pertinent :
C#
public class RestSharpJsonSerializer : ISerializer
{
    public string Serialize(object obj)
    {
        return JsonConvert.SerializeObject(obj, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.Indented });
    }

    public string RootElement { get; set; }
    public string Namespace { get; set; }
    public string DateFormat { get; set; }
    public string ContentType
    {
        get { return "application/json"; }
        set { }
    }
}


Result #1
"searchScope":"IndividualProvidersOnly",<br />
      "directoryIds":[<br />
         {<br />
            "string":"NGX"<br />
         },<br />
         {<br />
            "string":"MDA"<br />
         }<br />
      ],<br />
      "resultSizeLimit":10,


Result #2
"searchScope":"IndividualProvidersOnly",<br />
      "directoryIds":[<br />
         {<br />
            "string":"NGX",<br />
            "string":"MDA"<br />
         }<br />
      ],<br />
      "resultSizeLimit":10,


Ending the week like this is really bugging me so I'm looking forward to any help I can get over the next few days. Thank you everyone!
Posted
Comments
Mehdi Gholam 31-May-14 1:03am    
Shouldn't you be wanting :
"directoryIds" : [ "NGX", "MDA" ]
Mark-Harrington 31-May-14 9:22am    
You'd think that's what I wanted because that's what makes sense. :) But, that's not what this 2nd party service is expectin, unfortunately. Would it be fair to say that what I'm expecting is incorrectly formatted?
Mehdi Gholam 31-May-14 9:28am    
It's hard to get what you want through serialization. You could generate it by hand.
Mark-Harrington 31-May-14 9:38am    
That's what I was afraid of. I have about 16 classes that are rather large that need to be serialized. Doing that by hand would be reinventing the wheel. Is there any way to short-circuit the newtonsoft json serializer for that one class or would a find and replace string be a better option?
Mehdi Gholam 31-May-14 10:11am    
I would suggest serializing and doing the difficult bits by hand.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900