Deserialize JSON string to object that's also in the JSON - c#

{
"Cats": [
{
"Name": "Max",
"Color": "Brown"
},
{
"Name": "Cherry",
"Color": "Black"
}
],
"Owners": [
{
"Name": "Tom",
"Cat": "Max"
},
{
"Name": "Cindy",
"Cat": "Cherry"
}
]
}
Instead of getting the string value of Tom's Cat, and then doing a second operation to get the cat's color, is it possible to deserialize this JSON so that I can just do:
var tomsCatsColor = Rootobject.Owners[0].Cat.Color;

You can give the user a cat identifier, this will look something like:
{
"Cats": [
{
"Name": "Max",
"Color": "Brown"
},
{
"Name": "Cherry",
"Color": "Black"
}
],
"Owners": [
{
"Name": "Tom",
"Cat": 0
},
{
"Name": "Cindy",
"Cat": 1
}
]
}
var tomsCatsColor = Rootobject.Cats[Rootobject.Owners[0].Cat].Color;

Related

NjsonSchema Validation If Property1 is equal to Something then require Property2

I seem to not be able to get const or enum working as part of an if-then-else JSON schema validation.
They seem to be interchangeable when 1 validation value is concerned. (Reference)
Here is my JSON schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Test_Schema",
"description": "A schema for validating a test object",
"type": "object",
"additionalProperties": false,
"properties": {
"GeneralData": {
"type": "object",
"description": "Advsor and admin customer information",
"properties": {
"Name": {
"type": [ "string", "null" ],
"description": "Customer's advisor name"
},
"Age": {
"type": [ "string", "null" ],
"description": "Customer's advisor email"
},
"Location": {
"type": [ "string", "null" ],
"description": "The advisor's manager email'"
}
},
"required": [ "Name", "Location", "Age" ]
},
"ClientData": {
"type": "object",
"description": "Customer's information",
"additionalProperties": false,
"properties": {
"Title": {
"type": [ "string", "null" ]
},
"Forename": {
"type": [ "string", "null" ]
},
"Surname": {
"type": [ "string" ]
}
},
"required": [ "Title" ],
"if": {
"properties": {
"Forename": { "enum": [ "Soameonea" ] }
},
"required": [ "Forename" ]
},
"then": { "required": [ "Surname" ] },
"else": false
}
}
}
If Forename = "Someone" I want Surname to be required.
Here is my jsonObject after serialization:
{
"GeneralData": {
"Name": "Not Relevant",
"Age": "Not Relevant",
"Location": "Not Relevant"
},
"ClientData": {
"Title": "SomeTitle",
"Forename": "Someone",
"Surname": null
}
}
Validation code:
internal void ValidateDataObjectOnSchema()
{
var dataObject = PopulateDataObject();
var json = JsonConvert.SerializeObject(dataObject);
var result = GetSchema().Validate(json);
var errors = result.Select(x =>
{
return new Errors() { Title = x.Property, Description = x.Schema.Description };
});
var i = errors;
}
internal JsonSchema GetSchema()
{
return JsonSchema.FromFileAsync("Test/testSchema.json").Result;
}
Right now it still requires Surname, even though the enum Soameonea != Someone and I only require Title. <== Issue1
In the JSON schema if I set "Surname":{"type":["string","null]} then the error disappears and still I don't get the error if I then change the if condition for the Forename enum to "Someone". <== Issue 2
I cannot get a different validation output if I replace Enum with Const, So if I can get one to work I'm sure the other will follow.
I've found several answers to this question (Answer 1), I try to implement the same thing and for some reason, it fails in my case.
What am I missing?
In the JSON schema if I set "Surname":{"type":["string","null]} then the error disappears
A property with a null value still has a value. If you want to say that the property value must not only exist, but not be null, then add "type": "string" to your then condition.
Update:
As #Relequestial mentioned in one of the comments it seems NJson Schema only supports up to Draft 04, this is why if-then-else did not work and implication should be used instead. Below is my solution with an alternative package.
The same code and json work with the package Newtonsoft.Json.Schema (Reference)
Code:
internal void ValidateDataObjectOnSchemaWithNewtonSoftJson()
{
var dataObject = PopulateDataObject();
var settings = new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
};
var jsonDataObjectString = JsonConvert.SerializeObject(dataObject, settings);
JObject jsonDataObject = JObject.Parse(jsonDataObjectString);
var jsonSchemaFile = File.ReadAllText("Test/testSchema.json");
var schema = JSchema.Parse(jsonSchemaFile);
;
IList<ValidationError> messages;
var result = jsonDataObject.IsValid(schema, out messages);
var errors = GetReadableResult(result, messages);
}
private List<Errors> GetReadableResult(bool result, IList<ValidationError> messages)
{
var errors = new List<Errors>();
if (!result)
{
foreach (var error in messages)
{
if (error.ChildErrors.Count > 0)
{
errors.Add(
new Errors()
{
Path = error.ChildErrors.FirstOrDefault()?.Path,
Kind = error.ChildErrors.FirstOrDefault()?.Message
});
}
else
{
errors.Add(new Errors()
{
Path = error.Path,
Kind = error.Message
});
}
}
}
return errors;
}
JsonSchema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Test_Schema",
"description": "A schema for validating a test object",
"type": "object",
"additionalProperties": false,
"properties": {
"GeneralData": {
"type": "object",
"description": "Advsor and admin customer information",
"properties": {
"Name": {
"type": [ "string", "null" ],
"description": "Customer's advisor name"
},
"Age": {
"type": [ "string", "null" ],
"description": "Customer's advisor email"
},
"Location": {
"type": [ "string", "null" ],
"description": "The advisor's manager email'"
}
},
"required": [ "Name", "Location", "Age" ]
},
"ClientData": {
"type": "object",
"description": "Customer's information",
"additionalProperties": false,
"properties": {
"Title": {
"type": [ "string", "null" ]
},
"Forename": {
"type": "string"
},
"Surname": {
"type": [ "string" ]
}
},
"required": [ "Title" ],
"if": {
"properties": {
"Forename": { "enum": [ "Someone" ] }
},
"required": [ "Forename" ]
},
"then": { "required": [ "Surname" ] },
"else": {}
}
}
}
The only addition would be to make the GetReadableResult recursive for Errors which have multiple child items.

Performing an update on objects from an array by using object own values

Having a collection sampled by next:
{
"_id": {
"$oid": "6139b08ee5a3119445892f94"
},
"type": "bike",
"specs": [
{
"name": "giant",
"models": [
{
"name": "v1",
"categories": [
{
"name": "v11",
"price": 0
},
{
"name": "v12",
"price": 0.1
},
{
"name": "v13",
"price": 0.1
}
]
},
{
"name": "v2",
"categories": [
{
"name": "v21",
"price": 1
},
{
"name": "v22",
"price": 0.1
}
]
}
]
},
{
"name": "sputnik",
"models": [
{
"name": "s1",
"categories": [
{
"name": "s11",
"price": 20
},
{
"name": "s12",
"price": 0.9
},
{
"name": "s13",
"price": 1.1
}
]
},
{
"name": "s2",
"categories": [
{
"name": "s31",
"price": 1
},
{
"name": "s32",
"price": 0.1
}
]
}
]
}
]
}
In order to edit models items by adding a new subdocument with next structure:
"valid": {
"isValid": true,
"currentName": [value from name field]
}
and remove field name
Almost achieved that by using Aggregations:
var dineInOptionsDefinition =
BsonDocument.Parse("{ isValid: 5, currentName: '$specs.name'}");
var addNewFieldsDefinition = new BsonDocument
{
{ "$addFields", new BsonDocument
{
{
"specs.models.valid", dineInOptionsDefinition
}
}
}
};
var t1 = collection
.Aggregate()
.Match(filterDefinition)
.AppendStage<BsonDocument>(addNewFieldsDefinition)
.Merge(collection);
The problem I'm facing is the value for currentName - as result it's an array of concatenated name fields values. Any clue how to get the value from the current document field value?

Merge Multiple JSON and Multiple level of Group By in C#

I'm trying to merge multiple JSON files which has the same type of data inside them and fetch the merged data from those JSONs. For example below are two JSON files.
JSON 1
[
{
"Name": "Sample1",
"Data": [
{
"Name": "Sample1 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["278924"]
}
]
}
]
},
{
"Name": "Sample2",
"Data": [
{
"Name": "Sample2 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["278378"]
},
{
"Name": "YYY",
"ID": ["278289"]
}
]
}
]
}
]
JSON 2
[
{
"Name": "Sample1",
"Data": [
{
"Name": "Sample1 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["357896"]
}
]
}
]
},
{
"Name": "Sample2",
"Data": [
{
"Name": "Sample2 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["356842"]
},
{
"Name": "YYY",
"ID": ["357123"]
}
]
}
]
}
]
I'm expecting the output to be in the below format.
[
{
"Name": "Sample1",
"Data": [
{
"Name": "Sample1 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["278924, 357896"]
}
]
}
]
},
{
"Name": "Sample2",
"Data": [
{
"Name": "Sample2 Sub1",
"Data": [
{
"Name": "XXX",
"ID": ["278378,356842"]
},
{
"Name": "YYY",
"ID": ["278289,357123"]
}
]
}
]
}
]
I'm not sure where to start this. I tried groupby for multiple level but couldn't able to fetch the result in expected format.
Any help would be appreciated.

Deserialize Complex Json in C# using Json.net

I have some complex JSon that I am trying to parse into something meaningful. I'm attempting to deserialize using C# Json.net but I can't get the values that I need. What I need is the value from every ColData node except those in a "summary" section. I am able to deserialize into an object using but I am stuck there.
string pandltext = #"{
"Header": {
"Time": "2017-08-24T08:32:58-07:00",
"ReportName": "ProfitAndLoss",
"ReportBasis": "Accrual",
"StartPeriod": "2017-06-01",
"EndPeriod": "2017-06-30",
"SummarizeColumnsBy": "Total",
"Currency": "USD",
"Option": [
{
"Name": "AccountingStandard",
"Value": "GAAP"
},
{
"Name": "NoReportData",
"Value": "false"
}
]
},
"Columns": {
"Column": [
{
"ColTitle": "",
"ColType": "Account",
"MetaData": [
{
"Name": "ColKey",
"Value": "account"
}
]
},
{
"ColTitle": "Total",
"ColType": "Money",
"MetaData": [
{
"Name": "ColKey",
"Value": "total"
}
]
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Income"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Design income",
"id": "82"
},
{
"value": "975.00"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Discounts given",
"id": "86"
},
{
"value": "-30.50"
}
],
"type": "Data"
},
{
"Header": {
"ColData": [
{
"value": "Landscaping Services",
"id": "45"
},
{
"value": "360.00"
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Job Materials",
"id": "46"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Fountains and Garden Lighting",
"id": "48"
},
{
"value": "550.00"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Plants and Soil",
"id": "49"
},
{
"value": "1820.72"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Sprinklers and Drip Systems",
"id": "50"
},
{
"value": "30.00"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Job Materials"
},
{
"value": "2400.72"
}
]
},
"type": "Section"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Landscaping Services"
},
{
"value": "2760.72"
}
]
},
"type": "Section"
},
{
"ColData": [
{
"value": "Pest Control Services",
"id": "54"
},
{
"value": "-100.00"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Sales of Product Income",
"id": "79"
},
{
"value": "44.00"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Services",
"id": "1"
},
{
"value": "400.00"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Income"
},
{
"value": "4049.22"
}
]
},
"type": "Section",
"group": "Income"
},
{
"Summary": {
"ColData": [
{
"value": "Gross Profit"
},
{
"value": "4049.22"
}
]
},
"type": "Section",
"group": "GrossProfit"
},
{
"Header": {
"ColData": [
{
"value": "Expenses"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Automobile",
"id": "55"
},
{
"value": "19.99"
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Fuel",
"id": "56"
},
{
"value": "179.15"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Automobile"
},
{
"value": "199.14"
}
]
},
"type": "Section"
},
{
"Header": {
"ColData": [
{
"value": "Job Expenses",
"id": "58"
},
{
"value": "108.09"
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Job Materials",
"id": "63"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Decks and Patios",
"id": "64"
},
{
"value": "88.09"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Job Materials"
},
{
"value": "88.09"
}
]
},
"type": "Section"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Job Expenses"
},
{
"value": "196.18"
}
]
},
"type": "Section"
},
{
"Header": {
"ColData": [
{
"value": "Legal & Professional Fees",
"id": "12"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Accounting",
"id": "69"
},
{
"value": "75.00"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Lawyer",
"id": "71"
},
{
"value": "100.00"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Legal & Professional Fees"
},
{
"value": "175.00"
}
]
},
"type": "Section"
},
{
"ColData": [
{
"value": "Maintenance and Repair",
"id": "72"
},
{
"value": "185.00"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Meals and Entertainment",
"id": "13"
},
{
"value": "5.66"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Rent or Lease",
"id": "17"
},
{
"value": "900.00"
}
],
"type": "Data"
},
{
"Header": {
"ColData": [
{
"value": "Utilities",
"id": "24"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Gas and Electric",
"id": "76"
},
{
"value": "114.09"
}
],
"type": "Data"
},
{
"ColData": [
{
"value": "Telephone",
"id": "77"
},
{
"value": "74.36"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Utilities"
},
{
"value": "188.45"
}
]
},
"type": "Section"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Expenses"
},
{
"value": "1849.43"
}
]
},
"type": "Section",
"group": "Expenses"
},
{
"Summary": {
"ColData": [
{
"value": "Net Operating Income"
},
{
"value": "2199.79"
}
]
},
"type": "Section",
"group": "NetOperatingIncome"
},
{
"Header": {
"ColData": [
{
"value": "Other Expenses"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"ColData": [
{
"value": "Miscellaneous",
"id": "14"
},
{
"value": "916.00"
}
],
"type": "Data"
}
]
},
"Summary": {
"ColData": [
{
"value": "Total Other Expenses"
},
{
"value": "916.00"
}
]
},
"type": "Section",
"group": "OtherExpenses"
},
{
"Summary": {
"ColData": [
{
"value": "Net Other Income"
},
{
"value": "-916.00"
}
]
},
"type": "Section",
"group": "NetOtherIncome"
},
{
"Summary": {
"ColData": [
{
"value": "Net Income"
},
{
"value": "1283.79"
}
]
},
"type": "Section",
"group": "NetIncome"
}
]
}
}
// Deserialize to object
var rootObj = JsonConvert.DeserializeObject<ProfitLoss.Rootobject>( pandltext );
I've tried querying a JContainer like is mentioned in this post. I've tried deserlializing a fragment like is mentioned in the documentation and I've tried using linq as mentioned here in the documentation. So far all of my efforts have met varying degrees of "success" but none have yielded the values I'm trying to get. Eventually this data will be bound to a WPF DataGrid for viewing.
Edit:
Added entire Json file
These are a couple attempts to get something, but I run into null values in both cases.
// This always returns null
var results2 = doc.Descendants()
.OfType<JObject>()
.Where( x => x[ "value" ] != null );
// This gives me a null exception error
var doc1 = ( JContainer ) o[ "Rows" ];
foreach ( var row in rootObj.Rows.Row )
{
// Get a null exception
foreach ( var row2 in row.Rows.Row )
{
Console.WriteLine( row2.ToString() );
}
}
Edit 2:
Using what #Eser gave as a starting point, I am able to get a list of values, but unfortunately it's just a list of values. Instead of getting something like
"Design income", "975.00"
"Discounts given", "-30.50"
I get
"Design income"
"975"
"Discounts given"
"-30.50"
Here is the code I'm using to get a list of values:
var jObj = JObject.Parse( pandltext );
var results = jObj.SelectTokens( "$..Rows.Row[?(#.type == 'Data')]..value" ).ToList();
var jObj = JObject.Parse(json);
var colData = jObj.SelectTokens("$..ColData")
.Except(jObj.SelectTokens("$..Summary.ColData"))
.ToList();
EDIT
foreach(var item in colData)
{
Console.WriteLine(string.Join("=", item.Select(x => x["value"])));
}
or
var finalList = colData.Select(item => item.Select(x => (string)x["value"]).ToList())
.ToList();
    public class Option
    {
         public string Name { get; set; }
    public string Value { get; set; }
   }
    public class Header
    {
        public DateTime Time { get; set; }
        public string ReportName { get; set; }
        public string ReportBasis { get; set; }
        public string StartPeriod { get; set; }
        public string EndPeriod { get; set; }
        public string SummarizeColumnsBy { get; set; }
        public string Currency { get; set; }
        public IList<Option> Option { get; set; }
    }
    public class MetaData
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }
    public class Column
    {
        public string ColTitle { get; set; }
        public string ColType { get; set; }
        public IList<MetaData> MetaData { get; set; }
    }
    public class Columns
    {
        public IList<Column> Column { get; set; }
    }
    public class ColData
    {
        public string value { get; set; }
        public string id { get; set; }
    }
    public class ColData
    {
        public string value { get; set; }
        public string id { get; set; }
    }
    public class Row
   {
      public IList<ColData> ColData { get; set; }
        public string type { get; set; }
    }
    public class Rows
    {
        public IList<Row> Row { get; set; }
    }
    public class ColData
    {
        public string value { get; set; }
    }
    public class Summary
    {
        public IList<ColData> ColData { get; set; }
    }
    public class ColData
    {
        public string value { get; set; }
        public string id { get; set; }
    }
    public class Row
    {
        public  Header { get; set; }
        public Rows Rows { get; set; }
        public Summary Summary { get; set; }
        public string type { get; set; }
        public IList<ColData> ColData { get; set; }
    }
    public class Rows
    {
        public IList<Row> Row { get; set; }
    }
    public class Row
    {
        public IList<ColData> ColData { get; set; }
        public string type { get; set; }
        public  Header { get; set; }
        public Rows Rows { get; set; }
        public  Summary { get; set; }
    }
    public class Rows
    {
        public IList<Row> Row { get; set; }
    }
    public class Row
    {
        public  Header { get; set; }
        public Rows Rows { get; set; }
        public  Summary { get; set; }
        public string type { get; set; }
        public string group { get; set; }
    }
    public class Rows
    {
        public IList<Row> Row { get; set; }
    }
    public class Example
    {
        public Header Header { get; set; }
        public Columns Columns { get; set; }
        public Rows Rows { get; set; }
    }
and use it with :
Example results = Newtonsoft.JSON.JsonConvert.DeserializeObject<Example>(json);

How to handle error in Json.Net parsing

I'm using Json.Net for Json deserialization. On occasion the Json string I read is not correct (which I can't fix since I don't produce it). In particular, at one specific place, where there should be a string, sometimes there is a serialized object instead. Json.Net then complains, not surprisingly, about finding an object where it expected a string.
I found I can intercept this by using Error in JsonSerializerSettings and also make Json.Net ignore the problem by setting ErrorContext.Handled. But I want to do even more. If I could look at the serialised object I could figure out what the string should be and in theory supply the correct answer. In practice I can't figure our how to do so. Especially, in the error handler:
How do I access the string that the parser tripped up on (Note that the parser can successfully continue if ErrorContext.Handled is set, so it can determine start and end of the problem string correctly)?
How do I supply the correct string back to the parser, or alternatively get access to the currently parsed object so I can set the value manually?
[Edit]
As requested a simplified example:
The incorrect Json string I get to parse:
{
"id": 2623,
"name": {
"a": 39,
"b": 0.49053320637463277,
"c": "cai5z+A=",
"name": "22"
},
"children": [
{
"id": 3742,
"name": {
"a": 37,
"b": 0.19319664789046936,
"c": "Me/KKPY=",
"name": "50"
},
"children": [
{
"id": 1551,
"name": {
"a": 47,
"b": 0.6935373953047849,
"c": "qkGkMwY=",
"name": "9"
},
"children": []
},
{
"id": 4087,
"name": {
"a": 5,
"b": 0.42905938319352427,
"c": "VQ+yH6o=",
"name": "84"
},
"children": []
},
{
"id": 614,
"name": {
"a": 19,
"b": 0.7610801005554758,
"c": "czjTK1s=",
"name": "11"
},
"children": []
}
]
},
{
"id": 3382,
"name": {
"a": 9,
"b": 0.36416331043660793,
"c": "lnoHrd0=",
"name": "59"
},
"children": [
{
"id": 4354,
"name": {
"a": 17,
"b": 0.8741648112769075,
"c": "CD2i2I0=",
"name": "24"
},
"children": []
},
{
"id": 2533,
"name": {
"a": 52,
"b": 0.8839575992356788,
"c": "BxFEzVI=",
"name": "60"
},
"children": []
},
{
"id": 5733,
"name": {
"a": 4,
"b": 0.7230552787534219,
"c": "Un7lJGM=",
"name": "30"
},
"children": []
}
]
},
{
"id": 9614,
"name": {
"a": 81,
"b": 0.4015882813379114,
"c": "dKgyRZk=",
"name": "63"
},
"children": [
{
"id": 7831,
"name": {
"a": 81,
"b": 0.2784254314743101,
"c": "xZur64o=",
"name": "94"
},
"children": []
},
{
"id": 6293,
"name": {
"a": 73,
"b": 0.32629523068959604,
"c": "lMkosP4=",
"name": "93"
},
"children": []
},
{
"id": 5253,
"name": {
"a": 13,
"b": 0.19240453242901923,
"c": "oOPZ3tA=",
"name": "5"
},
"children": []
}
]
}
]
}
And here to class to parse it into:
class Node
{
[JsonProperty]
int id;
[JsonProperty]
string name;
[JsonProperty]
List<Node> children;
}
As you can see it expects a string name but sometimes incorrectly gets a serialised object (which contains the string in question as a member). This only happens in some JSON strings but not others, so I can't just change the class definition of Node to match.
[Edit 2]
A "correct" Json string according to my API would look like this:
{
"id": 2623,
"name": "22",
"children": [
{
"id": 3742,
"name": "50",
"children": [
{
"id": 1551,
"name": "9",
"children": []
},
{
"id": 4087,
"name":"84",
"children": []
},
...
Trying to detect errors after the fact and then reparse from the point of the error is going to be problematic, as you have seen. Fortunately, the problem you've described can be solved in a straightforward manner using a custom JsonConverter. The idea is to have the converter read the data into a temporary structure that can handle either form (object or string), query the type, then construct the Node from there.
Here is the code for the converter:
class NodeConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(Node));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
Node node = new Node();
node.id = (int)jo["id"];
JToken name = jo["name"];
if (name.Type == JTokenType.String)
{
// The name is a string at the current level
node.name = (string)name;
}
else
{
// The name is one level down inside an object
node.name = (string)name["name"];
}
node.children = jo["children"].ToObject<List<Node>>(serializer);
return node;
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
To use the converter, add a [JsonConverter] attribute to your Node class like this:
[JsonConverter(typeof(NodeConverter))]
class Node
{
public int id { get; set; }
public string name { get; set; }
public List<Node> children { get; set; }
}
Then you can deserialize as normal:
Node node = JsonConvert.DeserializeObject<Node>(json);
Here is a full demo showing the converter in action. For illustration purposes, I've created a new JSON string that contains a combination of the "good" and "bad" nodes you described in your question.
class Program
{
static void Main(string[] args)
{
string json = #"
{
""id"": 2623,
""name"": {
""a"": 39,
""b"": 0.49053320637463277,
""c"": ""cai5z+A="",
""name"": ""22""
},
""children"": [
{
""id"": 3741,
""name"": ""50"",
""children"": [
{
""id"": 1550,
""name"": ""9"",
""children"": []
},
{
""id"": 4088,
""name"": {
""a"": 5,
""b"": 0.42905938319352427,
""c"": ""VQ+yH6o="",
""name"": ""85""
},
""children"": []
}
]
},
{
""id"": 3742,
""name"": {
""a"": 37,
""b"": 0.19319664789046936,
""c"": ""Me/KKPY="",
""name"": ""51""
},
""children"": [
{
""id"": 1551,
""name"": {
""a"": 47,
""b"": 0.6935373953047849,
""c"": ""qkGkMwY="",
""name"": ""10""
},
""children"": []
},
{
""id"": 4087,
""name"": ""84"",
""children"": []
}
]
}
]
}";
Node node = JsonConvert.DeserializeObject<Node>(json);
DumpNode(node, "");
}
private static void DumpNode(Node node, string indent)
{
Console.WriteLine(indent + "id = " + node.id + ", name = " + node.name);
foreach(Node child in node.children)
{
DumpNode(child, indent + " ");
}
}
}
Output:
id = 2623, name = 22
id = 3741, name = 50
id = 1550, name = 9
id = 4088, name = 85
id = 3742, name = 51
id = 1551, name = 10
id = 4087, name = 84

Categories