JsonSerializer Isn't serializing all data - c#

Here's some simple code that deserializes a .json file then serializes it again, making no changes to the data.
JObject json = JObject.Parse(File.ReadAllText("fileIn.json"));
JsonWriter writer = new JsonTextWriter(new StreamWriter("fileOut.json", false));
writer.Formatting = Formatting.Indented;
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(writer, json);
Everything seems to be deserialized just fine as the json JObject contains all the data but strangely not everything is being serialized.
If this is fileIn.json:
{
"metadata":{
"vertices":56
},
"influencesPerVertex":2,
"bones":[{
"parent":-1,
"name":"torso",
"scl":[1,1,1],
"pos":[-2.42144e-08,0.720174,-0.00499988],
"rotq":[0.707107,0,-0,0.707107]
},{
"parent":0,
"name":"head",
"scl":[1,1,1],
"pos":[0,0,-0.904725],
"rotq":[0,0,-0,1]
},{
"parent":0,
"name":"leftLeg",
"scl":[1,1,1],
"pos":[0.173333,-4.05163e-05,-0],
"rotq":[1,-4.37114e-08,-0,0]
}],
"skinIndices":[1,2,3],
"vertices":[1,2,3],
"skinWeights":[1,2,3],
"faces":[1,2,3],
"normals":[1,2,3],
"uvs":[]
}
Then fileOut.json will look like this:
{
"metadata": {
"vertices": 56
},
"influencesPerVertex": 2,
"bones": [
{
"parent": -1,
"name": "torso",
"scl": [
1,
1,
1
],
"pos": [
-2.42144E-08,
0.720174,
-0.00499988
],
"rotq": [
0.707107,
0,
0,
0.707107
]
},
{
"parent": 0,
"name": "head",
"scl": [
1,
1,
1
],
"pos": [
0,
0,
-0.904725
],
"rotq": [
0,
0,
0,
1
]
},
{
"parent": 0,
"name": "leftLeg",
"scl": [
1,
1,
1
],
"pos": [
0.173333,
-4.05163E-05,
0
],
"rotq": [
1,
-4.37114E-08,
0,
0
]
}
],
"skinIndices": [
1,
2,
3
],
"vertices": [
1,
2,
3
As you can see the output file is missing data towards the end. Why is this happening and how can I fix it? Thanks

You don't close your output file (new StreamWriter("fileOut.json", false), this is why you don't see the whole file...
A simpler way for writing indented json back to file would be
JObject json = JObject.Parse(File.ReadAllText("fileIn.json"));
File.WriteAllText("fileOut.json", json.ToString(Newtonsoft.Json.Formatting.Indented));

Related

File writing: display Unicode characters in finished file? [duplicate]

This question already has answers here:
How to decode a Unicode character in a string
(3 answers)
Closed 1 year ago.
I'm trying to create flash cards to memorise Japanese kanji characters, and for that I'm crawling Jitenon, a website containing tons of kanji definitions, pronunciations and meanings. I've coded up the classes that would hold the relevant information that can be found on each kanji page, and I'm currently trying to save my list of kanji as a json file.
For testing purposes, I'm trying to parse individual kanji objects like this:
...
var kanji = scraper.GetKanjiDefinition(kanjiUrl);
var jsonOptions = new JsonSerializerOptions() { WriteIndented = true };
var kanjiJson = JsonSerializer.Serialize(kanji, jsonOptions);
File.WriteAllText("kanji_json.json", kanjiJson, Encoding.UTF8);
Here's an example page that I've crawled with its corresponding json serialization:
{
"Character": "\u697D",
"MainRadical": "\u6728",
"Strokes": 13,
"KankenLevel": "\uFF19\u7D1A",
"Education": "\u5C0F\u5B66\u6821\uFF12\u5E74\u751F",
"Meanings": [
{
"Indices": [
"\u301C"
],
"Meaning": "\u304A\u3093\u304C\u304F\u3002",
"JapanTypical": false
},
{
"Indices": [
"\u301C"
],
"Meaning": "\u304B\u306A\u3067\u308B\u3002\u97F3\u3092\u304B\u306A\u3067\u308B\u3002\u6F14\u594F\u3059\u308B\u3002",
"JapanTypical": false
},
{
"Indices": [
"\u301C"
],
"Meaning": "\u305F\u306E\u3057\u3044\u3002\u305F\u306E\u3057\u3080\u3002\u3088\u308D\u3053\u3076\u3002",
"JapanTypical": false
},
{
"Indices": [
"\u301C"
],
"Meaning": "\u3053\u306E\u3080\u3002\u611B\u3059\u308B\u3002\u306D\u304C\u3046\u3002\u6C42\u3081\u308B\u3002",
"JapanTypical": false
},
{
"Indices": [],
"Meaning": "\u65E5\u672C\u3089\u304F\u3002\u305F\u3084\u3059\u3044\u3002\u5FC3\u8EAB\u306B\u82E6\u75DB\u304C\u306A\u304F\u3001\u306E\u3073\u306E\u3073\u3059\u308B\u3002",
"JapanTypical": true
}
],
"Readings": [
{
"Reading": "\u30AC\u30AF",
"Yomi": 0,
"MeaningIndices": [
1
],
"Education": "\u5C0F"
},
{
"Reading": "\u30E9\u30AF",
"Yomi": 0,
"MeaningIndices": [
2
],
"Education": "\u5C0F"
},
{
"Reading": "\u30AE\u30E7\u30A6",
"Yomi": 0,
"MeaningIndices": [
3
],
"Education": "\u5C0F"
},
{
"Reading": "\u30B4\u30A6",
"Yomi": 0,
"MeaningIndices": [
3
],
"Education": "\u5C0F"
},
{
"Reading": "\u305F\u306E\uFF08\u3057\u3044\uFF09",
"Yomi": 1,
"MeaningIndices": [],
"Education": "\u5C0F"
},
{
"Reading": "\u305F\u306E\uFF08\u3057\u3080\uFF09",
"Yomi": 1,
"MeaningIndices": [],
"Education": "\u5C0F"
},
{
"Reading": "\u304B\u306A\uFF08\u3067\u308B\uFF09",
"Yomi": 1,
"MeaningIndices": [],
"Education": "\u5C0F"
},
{
"Reading": "\u3053\u306E\uFF08\u3080\uFF09",
"Yomi": 1,
"MeaningIndices": [],
"Education": "\u5C0F"
}
]
}
I'd like to have the actual Japanese text included in the json file, for example "Character": "楽" and "MainRadical": "木" and "KankenLevel": "9級" instead of the escaped Unicode characters like \u697D. How could I achieve this in .NET?
If it makes any difference, I'm on Ubuntu 20.04 LTS and I open my json files in VS Code 1.56.2.
Set the Encoder in your jsonOptions:
var jsonOptions = new JsonSerializerOptions() {
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
WriteIndented = true
};
The above one allows all UnicodeRanges

Parse Google Transliteration Json using c#

I'm trying to get value from Google transliteration Json. Json code is looking like this:
[
"SUCCESS",
[
[
"hlo",
[
"jaisa",
"jaisaa",
"jaissa",
"jaisaaa",
"zaisa",
"jaissaa"
],
[],
{
"candidate_type": [
0,
0,
0,
0,
0,
0
]
}
],
[
"ok",
[
"main",
"mainn",
"man",
"mein",
"mann",
"meinn"
],
[],
{
"candidate_type": [
0,
0,
0,
0,
0,
0
]
}
]
]
]
my C# code to get value from json:
var userObj = JObject.Parse(jsontxt.Text.Trim());
wordstxt.Text = userObj[1][0][0].ToString();
There is no variable name to extract value from json code, I'm getting error like this:
Error reading JObject from JsonReader. Current JsonReader item is not
an object: StartArray. Path '', line 1, position 1.
Can anyone help me to solve this problem. I'm trying to get value hlo > jaisa
As #JonSkeet mentioned in comment section. You can parse this Json to JArray then use userObj[1][0][1]; to get expected list.
static void Main(string[] args)
{
var jsonString = #"[
""SUCCESS"",
[
[
""hlo"",
[
""jaisa"",
""jaisaa"",
""jaissa"",
""jaisaaa"",
""zaisa"",
""jaissaa""
],
[],
{
""candidate_type"": [
0,
0,
0,
0,
0,
0
]
}
],
[
""ok"",
[
""main"",
""mainn"",
""man"",
""mein"",
""mann"",
""meinn""
],
[],
{
""candidate_type"": [
0,
0,
0,
0,
0,
0
]
}
]
]
]";
var userObj = JArray.Parse(jsonString);
var expectedListValues = userObj[1][0][1];
foreach (JToken item in expectedListValues)
{
Console.WriteLine(item.ToString());
}
Console.ReadLine();
}
The output will be:
jaisa
jaisaa
jaissa
jaisaaa
zaisa
jaissaa
If you want to just take first item of this, you can use userObj[1][0][1][0].ToString() as well.

Selenium - json - c#

var pre1 = driver.FindElementByTagName("pre").Text.Replace(#"\", "").Trim();
dynamic root = JsonConvert.DeserializeObject(pre1);
I have this JSON response:
{
"success": true,
"message": null,
"outright": false,
"eventId": 0,
"si": 111,
"leonard": [{
"catalog":[0,0,0,0,0,0],
"edit": 25965112,
"mkilo": {
"888;315;2;3;0": {
"id": 1000,
"description": "Car"
},
"888;316;2;4;0": {
"id": 1001,
"description": "Train"
},
"888;317;2;5;0": {
"id": 1002,
"description": "Airplane"
}
},
"ti": "008000",
"checkin": 254,
"searchCar": {
"id": 1000,
"description": "Car"
}
}],
"ti": 149498
}
verified with jsonlint
root.leonard[0].catalog.Count = 6 ---- > OK
but
root.leonard[0].mkilo.Count = null - -- Why?
I want to read the contents of mkilo.

ReadAsStringAsync only returns part of the JSON / BODY

I'm trying to setup an integration point to an Automatic License Plate Recognition (ALPR) system. I'm using OpenALPR.
On the local PC there is an agent with a IP camera installed that will read the license plate and send info to the cloud. In the cloud there is a WebHook integration point that will fire each time a license plate is registered.
In the OpenALPR webpage the WebHook documentations says:
WebHooks send an HTTP POST to your URL every time a plate is received or an alert is triggered. The POST body contains the license plate data.
I've tested the integration by sending the output to a WebHook test page found on this URL: https://webhook.site/
The result is shown at the end of this post. As can be seen the output is JSON format.
I've done a RESTful service and deployed it in Azure. My problem is that when I read the result, I don't get the full JSON result. Based on the test on the http://webhook.site, I know the WebHook provider (sender) sends all JSON data so I can't figure out why it's not fully received.
If there is a better way to receive the JSON result in my web app I'm happy to hear about it. Below is my take on how to get the job done. Except it does not work ! :-D
Can anyone tell me what I'm doing wrong and why only part of the result is read?
This is the part of the code that is called on a HTTP POST to my service.
// POST: api/OpenALP
public HttpResponseMessage Post([FromBody]string value)
{
var content = Request.Content;
Request.Content.LoadIntoBufferAsync().Wait();
string jsonContent = content.ReadAsStringAsync().Result;
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
EDIT: Don't know if it's important- but the debugging was done by publishing the service as "Debug" and the attaching a debugger. The content shown below was copied out from the break point.
This is what I get in string jsonContent
" \"color\": [{\"confidence\": 92.68022918701172, \"name\": \"white\"}, {\"confidence\": 2.699702501296997, \"name\": \"silver-gray\"}, {\"confidence\": 1.8385039567947388, \"name\": \"yellow\"}, {\"confidence\": 1.024902582168579, \"name\": \"gold-beige\"}, {\"confidence\": 0.5838126540184021, \"name\": \"green\"}], \"make\": [{\"confidence\": 16.192890167236328, \"name\": \"renault\"}, {\"confidence\": 12.871005058288574, \"name\": \"mercedes-benz\"}, {\"confidence\": 7.908616065979004, \"name\": \"volkswagen\"}, {\"confidence\": 4.901283264160156, \"name\": \"opel\"}, {\"confidence\": 4.583385467529297, \"name\": \"bmw\"}], \"body_type\": [{\"confidence\": 20.72427749633789, \"name\": \"sedan-standard\"}, {\"confidence\": 18.412166595458984, \"name\": \"van-mini\"}, {\"confidence\": 17.614177703857422, \"name\": \"sedan-compact\"}, {\"confidence\": 12.435933113098145, \"name\": \"sedan-wagon\"}, {\"confidence\": 6.872223377227783, \"name\": \"suv-crossover\"}], \"year\": [{\"confidence\": 23.91012191772461, \"name\": \"2005-2009\"}, {\"confidence\": 19.78921890258789, \"name\": \"2000-2004\"}, {\"confidence\": 16.811452865600586, \"name\": \"1995-1999\"}, {\"confidence\": 11.53968620300293, \"name\": \"2010-2014\"}, {\"confidence\": 8.91786003112793, \"name\": \"1985-1989\"}], \"make_model\": [{\"confidence\": 2.5598113536834717, \"name\": \"renault_kangoo\"}, {\"confidence\": 1.564223289489746, \"name\": \"fiat_doblo\"}, {\"confidence\": 1.5174164772033691, \"name\": \"alfa-romeo_147\"}, {\"confidence\": 1.0350353717803955, \"name\": \"citroen_2cv\"}, {\"confidence\": 0.986167311668396, \"name\": \"volkswagen_caddy\"}]}, \"best_uuid\": \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355349\", \"epoch_end\": 1521329358227, \"best_image_width\": 640, \"data_type\": \"alpr_group\", \"best_image_height\": 480, \"frame_end\": 865634, \"is_parked\": false, \"web_server_config\": {\"agent_label\": \"DESKTOP-B3PMA1C\", \"camera_label\": \"WebCamHKA\"}, \"best_region\": \"eu-dk\", \"uuids\": [\"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355147\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355183\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355248\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355284\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355349\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355386\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355451\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355523\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355558\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355700\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355777\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355815\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329355889\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329356029\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329356274\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329356421\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329356598\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329356738\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329356773\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329356874\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329356909\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357014\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357049\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357148\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357387\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357422\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357567\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357631\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357734\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357870\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329357976\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329358012\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329358048\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329358084\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329358155\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329358191\", \"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521329358227\"], \"plate_indexes\": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], \"travel_direction\": 255.1732177734375, \"country\": \"eu\", \"best_plate_number\": \"AF22454\", \"best_region_confidence\": 91.45714569091797, \"agent_version\": \"2.5.103\", \"candidates\": [{\"matches_template\": 0, \"plate\": \"AF22454\", \"confidence\": 94.99976348876953}, {\"matches_template\": 0, \"plate\": \"AF2245\", \"confidence\": 69.99081420898438}, {\"matches_template\": 0, \"plate\": \"AF224\", \"confidence\": 68.92207336425781}, {\"matches_template\": 0, \"plate\": \"A22454\", \"confidence\": 65.7693099975586}, {\"matches_template\": 0, \"plate\": \"AY22454\", \"confidence\": 65.72945404052734}, {\"matches_template\": 0, \"plate\": \"AT22454\", \"confidence\": 65.69474029541016}]}"
If I look at the output on the WebHook test site (http://webhoot.site) I get this result:
{
"epoch_start": 1521286407536,
"camera_id": 645063384,
"frame_start": 109416,
"agent_uid": "WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X",
"best_confidence": 94.99918365478516,
"company_id": "d1806fef-5549-4915-86a8-1b1f975e8df9",
"version": 2,
"agent_type": "alprd",
"best_plate": {
"plate": "AF25463",
"confidence": 94.99918365478516,
"region_confidence": 99,
"vehicle_region": {
"y": 1,
"x": 80,
"height": 479,
"width": 479
},
"region": "eu-dk",
"plate_index": 0,
"processing_time_ms": 40.263999938964844,
"candidates": [
{
"matches_template": 0,
"plate": "AF25463",
"confidence": 94.99918365478516
}
],
"coordinates": [
{
"y": 248,
"x": 286
},
{
"y": 251,
"x": 599
},
{
"y": 318,
"x": 599
},
{
"y": 314,
"x": 286
}
],
"matches_template": 0,
"requested_topn": 10
},
"vehicle": {
"orientation": [
{
"confidence": 48.20035171508789,
"name": "270"
},
{
"confidence": 26.80327606201172,
"name": "225"
},
{
"confidence": 10.317419052124023,
"name": "315"
},
{
"confidence": 5.075159072875977,
"name": "180"
},
{
"confidence": 4.022528648376465,
"name": "0"
}
],
"color": [
{
"confidence": 89.68812561035156,
"name": "blue"
},
{
"confidence": 3.044862747192383,
"name": "silver-gray"
},
{
"confidence": 2.5986762046813965,
"name": "white"
},
{
"confidence": 2.5680367946624756,
"name": "green"
},
{
"confidence": 0.8009989857673645,
"name": "purple"
}
],
"make": [
{
"confidence": 11.725887298583984,
"name": "renault"
},
{
"confidence": 10.687095642089844,
"name": "land-rover"
},
{
"confidence": 9.455676078796387,
"name": "nissan"
},
{
"confidence": 7.793033123016357,
"name": "citroen"
},
{
"confidence": 6.8527727127075195,
"name": "suzuki"
}
],
"body_type": [
{
"confidence": 19.176368713378906,
"name": "suv-crossover"
},
{
"confidence": 18.331125259399414,
"name": "suv-standard"
},
{
"confidence": 18.325965881347656,
"name": "truck-standard"
},
{
"confidence": 11.761542320251465,
"name": "sedan-wagon"
},
{
"confidence": 10.666444778442383,
"name": "sedan-compact"
}
],
"year": [
{
"confidence": 19.741708755493164,
"name": "2005-2009"
},
{
"confidence": 17.747791290283203,
"name": "2000-2004"
},
{
"confidence": 16.381860733032227,
"name": "1995-1999"
},
{
"confidence": 13.306379318237305,
"name": "1985-1989"
},
{
"confidence": 11.591310501098633,
"name": "1980-1984"
}
],
"make_model": [
{
"confidence": 7.200094223022461,
"name": "land-rover_defender"
},
{
"confidence": 2.6545894145965576,
"name": "land-rover_discovery"
},
{
"confidence": 1.7968538999557495,
"name": "toyota_land-cruiser"
},
{
"confidence": 1.6618626117706299,
"name": "suzuki_sj-samurai"
},
{
"confidence": 1.4205354452133179,
"name": "suzuki_vitara"
}
]
},
"best_uuid": "WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410173",
"epoch_end": 1521286411970,
"best_image_width": 640,
"data_type": "alpr_group",
"best_image_height": 480,
"frame_end": 109536,
"is_parked": false,
"web_server_config": {
"agent_label": "DESKTOP-B3PMA1C",
"camera_label": "WebCamHKA"
},
"best_region": "eu-dk",
"uuids": [
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286407536",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286407571",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286407607",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286407783",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286407888",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286407991",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408267",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408301",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408335",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408579",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408649",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408860",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408895",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408930",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408964",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286408999",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286409173",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286409278",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286409417",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286409484",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286409651",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286409789",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286409823",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286409999",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410036",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410140",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410173",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410208",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410646",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410751",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410887",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410923",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286410957",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286411099",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286411169",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286411204",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286411523",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286411626",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286411694",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286411797",
"WBFOY6ZDPZR03FGCE5C1S9J5GIH2TFH1H6QPMX7X-645063384-1521286411970"
],
"plate_indexes": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"travel_direction": 18.316375732421875,
"country": "eu",
"best_plate_number": "AF25463",
"best_region_confidence": 97.08333587646484,
"agent_version": "2.5.103",
"candidates": [
{
"matches_template": 0,
"plate": "AF25463",
"confidence": 94.99918365478516
},
{
"matches_template": 0,
"plate": "AF2546",
"confidence": 70.50904846191406
},
{
"matches_template": 0,
"plate": "AF254",
"confidence": 68.68018341064453
},
{
"matches_template": 0,
"plate": "AF254AF",
"confidence": 65.75811767578125
},
{
"matches_template": 0,
"plate": "A25463",
"confidence": 65.72001647949219
},
{
"matches_template": 0,
"plate": "AE25463",
"confidence": 65.68501281738281
}
]
}
EDIT:
Ohhh - I can't post an Image. I need at least 10 reputation to post it.
But I can link to it here: https://klitandersen-my.sharepoint.com/:i:/g/personal/henrik_klit-andersen_com/Ec7DQkhrcFhAgZ6wIF24MbkBgrPcbXsgSa7_msXLZDHXQw?e=cnluHm
It appears that I was on the right track thinking that this is because you have a [FromBody] string value parameter in your controller method. That seems to offset the content buffer by 1024 bytes hence why your attempt to read it as a string inside the method body yielded only part of the original request body.
You could try a few things here (at least that's the ones I can think of). Firstly, you should try to remove the parameter from. That would solve this problem right away and you would be able to just read it without any issues as you were trying now with just a single line.
var postBodyString = Request.Content.ReadAsStringAsync().Result;
Secondly, you could try to read it as a stream and reset the stream's position. If you do go with this option - make sure you use using so that everything is disposed once you are done. I know for a fact that this didn't work for me some time ago because the ReadAsStreamAsync() in Web Api is a HttpBufferlessStream and I think it doesn't support Seek operation.
var postStream = Request.Content.ReadAsStreamAsync().Result;
postStream.Seek(0, SeekOrigin.Begin);
var postBodyString = new StreamReader(postStream).ReadToEnd();
Lastly, you could try and make the parameter HttpMessageRequest which you could then read as a string.
[Route("somePost")]
[HttpPost]
public IHttpActionResult DoSomething(HttpRequestMessage request)
{
var str = request.Content.ReadAsStringAsync().Result;
return Ok();
}
Hopefully some of these options will be acceptable for you or at least get you going on the right track.

Derserializing Json Error : Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'

I am trying to deserialize the Json object and it is throwing me the error Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'.
The error is surely the new piece below as before adding , it was de-serializing correctly. I think it could be StressTestResults which has key value pair values and probably not an array.
New Json added that led to the error above
{
"StressTestAnalysis":{
"StressTestResults": {
"curlevel":0,
"decrease":0,
"increase":0
},
"StressTestGraphData": {
"yaxis": [
1000000,
1500000,
2000000,
2500000,
3000000,
14000000
],
"data": [
20.00,
18.18,
16.53,
15.03,
13.66,
12.42,
11.29,
10.26,
1.68
]
}
}
}
Complete Json
{
"NpvResults":{
"CommIns":[
[
-4137.0277805691812,
82.740555611385162,
1,
-4054.2872249578099
],
[
-4137.0277805691812,
82.740555611385162,
2,
-4054.2872249578099
],
[
-4137.0277805691812,
82.740555611385162,
3,
-4054.2872249578099
],
[
-4137.0277805691812,
82.740555611385162,
4,
-4054.2872249578099
],
[
-4137.0277805691812,
82.740555611385162,
5,
-4054.2872249578099
]
],
"CaptiveIns":[
[
-4221.4569189482418,
84.429138378963501,
22.710059979002764,
-0.019802950859533291,
0,
0,
1,
-4114.337523541084
],
[
-4221.4569189482418,
84.429138378963501,
22.752243205547771,
0,
0,
0,
2,
-4114.275537363681
],
[
-4221.4569189482418,
84.429138378963501,
22.749463056422599,
0,
0,
0,
3,
-4114.2783175128006
],
[
-4221.4569189482418,
84.429138378963501,
22.829863952642476,
0,
0,
0,
4,
-4114.1979166165802
],
[
-4221.4569189482418,
84.429138378963501,
22.813678007288622,
0,
0,
0,
5,
-4114.2141025619358
],
[
0,
0,
0,
0,
0,
0,
6,
0
]
],
"SelfIns":[
[
-18.283680484373004,
0.36567360968745993,
1,
-17.918006874685538
],
[
-17.950738378065587,
0.35901476756131162,
2,
-17.59172361050425
],
[
-17.528414768410823,
0.35056829536821654,
3,
-17.177846473042624
],
[
-17.199888318302875,
0.343997766366058,
4,
-16.855890551936845
],
[
-16.962838081589766,
0.33925676163179552,
5,
-16.623581319957964
]
],
"GraphData":[
[
-20271.436124789201,
-20271.436124789201,
-20271.436124789201,
-20271.436124789201,
-20271.436124789201
],
[
-20575.839967122232,
-20573.231848189709,
-20571.270058283782,
-20569.376102934919,
-20567.025718278568
],
[
-149.31279568973736,
-116.11250599884572,
-85.587513928638344,
-55.97322678189083,
-26.126318881224275
]
],
"LowestCost":[
{
"options":"CommIns",
"prob":0
},
{
"options":"CaptiveIns",
"prob":1
},
{
"options":"SelfIns",
"prob":0
}
]
},
"EvaResults":{
"CaptiveView":[
[
1,
1004.6356155327186,
0.019999999999999664,
50231.780776636006
],
[
2,
1024.6444682949152,
1004.6556155327187,
1.0199087932436166
],
[
3,
1045.3665533832836,
2029.3000838276355,
0.51513987811914741
],
[
4,
1066.2458049620784,
3074.6666372109171,
0.34678575400775757
],
[
5,
1087.6618426753635,
4140.9124421729957,
0.26266321517513735
]
],
"ParentView":[
[
1,
-0.00040000000000000734,
7500,
-2000,
35000,
40499.999600000519,
0.019999999999999664,
2024999.9800000158
],
[
2,
-20.093112310654377,
7650,
-2060,
35000,
40569.906887689343,
1004.6556155327187,
40.382410074732441
],
[
3,
-40.586001676552755,
7803,
-2121.1999999999698,
35000,
40641.213998323416,
2029.3000838276355,
20.027338095426597
],
[
4,
-61.493332744218421,
7959.0599999998312,
-2183.6240000000166,
35000,
40713.94266725576,
3074.6666372109171,
13.241799735750419
],
[
5,
-82.81824884345987,
8118.241200000044,
-2247.2964800000163,
35000,
40788.126471156589,
4140.9124421729957,
9.8500647047036747
]
],
"GraphData":[
[
49809.117488777483,
50104.699097064135,
50235.788311093122,
50362.666684675249,
50684.163143253129
],
[
2025082.8666373945,
2025083.3307724716,
2025083.4888383881,
2025083.6416729619,
2025084.0321642612
]
],
"LowestCost":[
{
"options":"CaptiveView",
"prob":1
},
{
"options":"ParentView",
"prob":0
}
]
},
"EndingSurplusResults":{
"EndingSurplusData": {
"perc25":6583.7612487804181,
"perc50":7533.9888417518869,
"mean":7568.2880884950882,
"perc75":8491.6674708256032,
"perc90":9289.8510640642089,
"perc95":9610.0343551595361
},
"GraphData": [
8576.4652724267216,
9410.545525237445,
9571.0108458564246,
10038.966929461712,
9405.1536272893627,
10079.743965285403,
8393.2949227139325,
8908.5518442208522,
9293.0264829383468,
9722.8583794184997,
9141.0634609955559,
8364.1029286600969,
8502.6875425273283,
8976.167984801772,
7877.4787333027598,
8952.7424983933197,
9201.9696148434487,
8847.4973060152206,
9747.7562848962589,
8962.9037815079791,
9022.1384539308201,
5681.1573013298594
]
},
"FinancialStatements":{
"IncomeStatement":[
[
1,
4178.1932500000103,
85.269249999998678,
4263.4624999999414,
18.465612134335959,
50.173097169011001,
170.53849999999736,
3000,
1024.2852906966514,
0.85309250000000236,
-4642.3097170227602,
1025.138383196653,
-3617.171333826107,
20.502767663933046,
-72.343426676522142,
1004.6356155327186,
1004.6356155327186,
-92.84619434045527,
0.016099287680693068,
0.74365342723195738,
0.75975271491266771
],
[
2,
4261.7571149999503,
86.974635000000731,
4348.7317499999263,
18.491944231444947,
51.605102225783959,
173.94927000000146,
3060,
1044.6854335427724,
0.87014635000001883,
-4826.5589078423809,
1045.5555798927724,
-3781.0033279496056,
20.911111597855385,
-75.620066558992093,
1024.6444682949152,
1024.6444682949152,
-189.37737249730282,
0.016118963064858854,
0.74365342723195738,
0.75977239029683274
],
[
3,
4346.9922572999239,
88.714127700000333,
4435.7063850000332,
18.418025530043888,
52.847080670134723,
177.42825540000067,
3121.2000000000389,
1065.8130233998197,
0.88754127699999308,
-4893.8329583179911,
1066.7005646768214,
-3827.1323936411636,
21.334011293536406,
-76.542647872823409,
1045.3665533832836,
1045.3665533832836,
-287.25403166366249,
0.016066236133476294,
0.74365342723195738,
0.75971966336545038
],
[
4,
4433.932102446006,
90.488410253999461,
4524.4205127000223,
18.434282054184305,
54.284770809664288,
180.97682050799892,
3183.6239999999598,
1087.1006393281507,
0.90528410254000691,
-5189.5034432464236,
1088.0059234306907,
-4101.4975198157326,
21.760118468613825,
-82.029950396314604,
1066.2458049620784,
1066.2458049620784,
-391.04410052859066,
0.016072567229267684,
0.74365342723195738,
0.75972599446124212
],
[
5,
4522.610744494943,
92.298178459080859,
4614.9089229540396,
18.543823690342677,
55.536620991960014,
184.59635691816172,
3247.2964800000173,
1108.9356413535372,
0.92338178459078979,
-5236.1929873248337,
1109.8590231381279,
-4126.3339641867033,
22.197180462762532,
-82.526679283733927,
1087.6618426753635,
1087.6618426753635,
-495.76796027508777,
0.016052417484088474,
0.74365342723195738,
0.7597058447160624
]
],
"CashFlow":[
[
1,
4263.4624999999414,
18.465612134335959,
3000,
-72.343426676522142,
170.53849999999736,
1146.8018145421861,
-22.936036290843688,
0.85309250000000236,
0.019999999999999664,
1124.7388707513417,
0,
0
],
[
2,
4348.7317499999263,
18.491944231444947,
3060,
-75.620066558992093,
173.94927000000146,
1171.9106023275467,
-23.438212046550877,
0.87014635000001883,
0,
2274.081407382339,
1124.7388707513417,
0
],
[
3,
4435.7063850000332,
18.418025530043888,
3121.2000000000389,
-76.542647872823409,
177.42825540000067,
1195.20275194278,
-23.904055038855581,
0.88754127699999308,
0,
2321.5287748119181,
1149.3425366309982,
0
],
[
4,
4524.4205127000223,
18.434282054184305,
3183.6239999999598,
-82.029950396314604,
180.97682050799892,
1223.4153605341307,
-24.46830721068261,
0.90528410254000691,
0,
2372.0385756069109,
1172.1862381809224,
0
],
[
5,
4614.9089229540396,
18.543823690342677,
3247.2964800000173,
-82.526679283733927,
184.59635691816172,
1246.9989416292328,
-24.939978832584625,
0.92338178459078979,
0,
2422.8346820072279,
1199.8523374259858,
0
]
],
"BalanceSheet":[
[
1,
0.019999999999999664,
-92.84619434045527,
-22.936036290843688,
50.173097169011001,
1004.6356155327186,
0.019999999999999664,
835.63864999998646,
0,
0.019999999999999664,
1004.6556155327187,
0,
0,
1004.6756155327198,
1054.82871270173,
1124.7388707513417,
1101.8228344604984
],
[
2,
0.019999999999999664,
-189.37737249730282,
-46.37424833739469,
101.77819939479492,
1024.6444682949152,
0.019999999999999664,
852.35142299998779,
1004.6556155327187,
0,
2029.3000838276355,
0,
0,
2029.3200838276339,
2131.0782832224281,
2274.081407382339,
2227.7271590449436
],
[
3,
0.019999999999999664,
-287.25403166366249,
-70.278303376250193,
154.62528006492957,
1045.3665533832836,
0.019999999999999664,
869.39845145997833,
2029.3000838276355,
0,
3074.6666372109171,
0,
0,
3074.686637210918,
3229.2919172758507,
2321.5287748119181,
2251.2704714356696
],
[
4,
0.019999999999999664,
-391.04410052859066,
-94.746610586932803,
208.91005087459402,
1066.2458049620784,
0.019999999999999664,
886.78642048919619,
3074.6666372109171,
0,
4140.9124421729957,
0,
0,
4140.9324421729971,
4349.8224930475881,
2372.0385756069109,
2277.3119650199792
],
[
5,
0.019999999999999664,
-495.76796027508777,
-119.68658941951738,
264.44667186655391,
1087.6618426753635,
0.019999999999999664,
904.52214889900233,
4140.9124421729957,
0,
5228.5742848483651,
0,
0,
5228.5942848483655,
5493.0209567149213,
2422.8346820072279,
2303.1680925877076
]
]
},
"StressTestAnalysis":{
"StressTestResults": {
"curlevel":0,
"decrease":0,
"increase":0
},
"StressTestGraphData": {
"yaxis": [
1000000,
1500000,
2000000,
2500000,
3000000,
14000000
],
"data": [
20.00,
18.18,
16.53,
15.03,
13.66,
12.42,
1.68
]
}
},
"PackList":[
{
"tblname":"CommIns",
"cols":"premPaid;taxDeduction;year;netCostProj",
"Item":"NpvResults"
},
{
"tblname":"CaptiveIns",
"cols":"premPaid;taxDeduction;loanToParent;capContriDistr;dividendDistr;terminalVal;year;netCostProj",
"Item":"NpvResults"
},
{
"tblname":"SelfIns",
"cols":"discountedLossesPaid;discountedTaxDeduction;year;netCostProj",
"Item":"NpvResults"
},
{
"tblname":"GraphData",
"cols":"CommIns;CaptiveIns;SelfIns",
"Item":"NpvResults"
},
{
"tblname":"CaptiveView",
"cols":"year;captiveNetIncome;surplusDeployed;impliedRoi",
"Item":"EvaResults"
},
{
"tblname":"ParentView",
"cols":"year;costOfCapDeployed;costOfRiskArb;incrCost;npvTaxBenefit;outcome;surplusDeployed;impliedRoi",
"Item":"EvaResults"
},
{
"tblname":"GraphData",
"cols":"CaptiveView;ParentView",
"Item":"EvaResults"
},
{
"tblname":"IncomeStatement",
"cols":"year;directPremWritten;cededPremWritten;netPremWritten;lossesLaePaid;chgResIbnr;othrUwExp;gaExp;uwGain;netInvestmentIncome;irsDiscount;preTaxOpIncomeWithIrs;preTaxOpIncomeWithoutIrs;fedIncomeTaxWithIrs;fedIncomeTaxWithoutIrs;netOperatingIncome;netIncome;deferredTaxAssets;lossLaeRatio;expRatio;netCombinedRatio",
"Item":"FinancialStatements"
},
{
"tblname":"CashFlow",
"cols":"year;premCollected;lossLaePaid;gaExpPaid;fedIncomeTaxPaid;othrUwExpPaid;netCashByOpActivities;loanBack;invPurchasedSold;capPaidIn;cashInvEndOfYear;cashInvStartOfYear;dividendsPaid",
"Item":"FinancialStatements"
},
{
"tblname":"BalanceSheet",
"cols":"year;othrAssets;defFedIncTax;loanbackAsset;totLiabilities;retainedEarnings;nonControllingInt;capAdequacyReqd;capital;addnPaidInCap;shareholderEquity;additionallyNeeded;distribute;totShareholderEquity;totLiabilitiesTotShEquity;totCashInvestment;totAssets",
"Item":"FinancialStatements"
}
]
}
Trigger2Output class that maps to the Json
[JsonConverter(typeof(MinifyingJsonConverter))]
public class StressTestAnaylsis
{
public StressTestResults StressTestResults { get; set; }
public StressTestGraphData StressTestGraphData { get; set; }
}
public class StressTestResults
{
[JsonMinify]
public double curlevel { get; set; }
[JsonMinify]
public double decrease { get; set; }
[JsonMinify]
public double increase { get; set; }
}
public class StressTestGraphData
{
[JsonMinify]
public List<int> yaxis { get; set; }
[JsonMinify]
public List<double> data { get; set; }
}
Method that deserialises
public void Trigger2_Output_Deserialize_JSON_Minified()
{
var json = File.ReadAllText("Models\\Trigger2OutputMinified.json");
var o = JObject.Parse(json);
var tmp = JsonConvert.DeserializeObject<Trigger2Output>(o.ToString());
}

Categories

Resources