0

I am using Newtonsoft to deserialize JSON from a REST call to a C# object. The object is a list of Person. The Person has a lot of properties but right now I am only storing some of them. I want to have a string property on the Person that contains the JSON that makes up the whole person. Is there a way to do this? I am writing it back to a SQL database for now and I don't need the values but want to have it for future use if needed.

Object class

public class Worker
{
    public string associateOID { get; set; }
    public WorkerID workerID { get; set; }
    public Person person { get; set; }
    public WorkerDates workerDates { get; set; }
    public WorkerStatus workerStatus { get; set; }
    public List<WorkAssignment> workAssignments { get; set; }
    public CustomFieldGroup customFieldGroup { get; set; }
    public BusinessCommunication businessCommunication { get; set; }
    public string JSON { get; set; }
}

public class Meta
{
    public int totalNumber { get; set; }
}

public class WorkerResult
{
    public List<Worker> workers { get; set; }
    public Meta meta { get; set; }
}

My existing call to deserialize:

WorkerResult result = JsonConvert.DeserializeObject<WorkerResult>(json);
Jason Webber
  • 323
  • 1
  • 4
  • 14
  • Sure it is possible. What is the issue you are having? Do you have any code written? – CodingYoshi Apr 08 '18 at 21:55
  • I updated my original to include the object classes and my code for the Deserialize. Not really sure where to go from there. Didn't see any attributes to do it. – Jason Webber Apr 08 '18 at 22:00
  • With NewtonSoft, this is straight forward. Just set the `JSON` property of your class to the `JsonConvert.SerializeObject`. – CodingYoshi Apr 08 '18 at 22:07
  • Within my Worker class I am only grabbing some of the properties from the JSON, but want to store all JSON with properties for the database. – Jason Webber Apr 08 '18 at 22:17
  • 1
    It's doable with a custom `JsonConverter`, but would [`[JsonExtensionData]`](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonExtensionDataAttribute.htm) suit your needs better? See [How to serialize a Dictionary as part of its parent object using Json.Net](https://stackoverflow.com/a/23786127/3744182) and [Deserialize json with known and unknown fields](https://stackoverflow.com/a/21763919/3744182). – dbc Apr 08 '18 at 22:27

2 Answers2

4

I think you mean that you want to store all attributes from the JSON into your c# object - so that you can access them later if you need to.

To do this, you use the [JsonExtensionData] annotation.

public class Worker
{
    public string associateOID { get; set; }
    // Any other attributes that you want to use as .NET types go here

    // all attributes that don't have a property will be put into this dictionary
    // either as primitive types, or as objects of type Newtonsoft.Json.Linq.JObject
    // supports nested objects of any Json structure, and will serialize correctly.
    [JsonExtensionData]
    public Dictionary<string,object> ExtraAttributes {get;set;}
}

You can see a full example at https://dotnetfiddle.net/N5SuCY.

To store these properties in a database, you can combine this with a calculated string property:

[JsonIgnore]
public string SerializedExtraAttributes => JsonConvert.SerializeObject(ExtraAttributes);
gnud
  • 77,584
  • 5
  • 64
  • 78
1

Add JsonIgnore attribute to your JSON property like this:

[JsonIgnore()]
public string JSON { get; set; }

You can use do this after you have deserialized the object

JObject workerResult = JObject.Parse(json);

// this should contain a list of all the workers
IList<JToken> workers = workerResult["workers"].Children().ToList();

After that, iterate all the workers in the result object you obtained earlier and set the JSON property to the equivalent worker object

for (int i = 0; i < result.workers.Count; i++)
    result.workers[i].JSON = workers[i].ToString();
Shaked Dahan
  • 402
  • 5
  • 22