c# - JSON.NET parse optional values without try/catch -
so company i've been working has asked me make c# library client company needs access our api .net. far have working library, keep having problems parsing optional properties. use mongodb on backend, , mongodb doesn't store properties aren't provided if they're defined in schema.
for example, schema may like:
{ name:string, id:number, phone:string, email:string, experience:[] } where make document:
{ name:"joe", id:5, phone:"222-222-2222", } the properties email , experience not exist in document, json looks displayed above. these values not required values however, still need parse rest of it. problem when parse code above possible values when parse email or experience parser throws null reference exception, , reason because value i'm trying parse not exist , way reference these values so:
jobject o=jobject.parse(json); //parse json jobject json object string name=(string)o["name"]; int id=(int)o["id"]; string phone=(string)o["phone"]; string email=(string)o["emain"]; list<string> exp=o["experience"].select(t => (string)t).tolist(); now code lot more objective, using linq create object called job , object jobs store jobject in way can query values out of using methods of jobs object thats initialized original json string.
thing way can think of handle optional properties in json try/catch each , every value. seems sloppy , json have parse 40-50 properties in total. seems extremely slow , huge mess of code. i'm wondering if can implement cleaner more efficient way.
unless there reason can't use generic deserialize/serialize methods recommend changing approach use those. in general, think type of conditional, property specific parsing you're doing above poor practice. here example;
public class job { public string name; public string id; public string phone; public string email; public string[] experience; // can list<string> without problems } job j = jsonconvert.deserializeobject<job>(jsonstring); string output = jsonconvert.serializeobject(j); //will include "optional" parameters, meaning if there no phone value empty string property name still there. if want confirm say, required parameters included other optional ones not recommend using json schemas in combination json.net. can following;
//schema in flat text file read file.readalltext(path); { "type":"object", "$schema": "http://json-schema.org/draft-03/schema", "required":true, "properties":{ "name": { "type":"string", "required":true }, "id": { "type":"string", "required":true }, "phone": { "type":"string", "required":false }, "email": { "type":"string", "required":false }, "experience": { "type":"array", "required":true, "items": { "string" } } } } then in code have like;
jobject obj = jobject.parse(json); jsonschema jscheme = jsonschema.parse(file.readalltext(thatschemaabove)); ilist<string> errors; obj.isvalid(jscheme, out errors); if (errors.count() > 0) { //json didn't match schema, it! } edit: handling complex objects;
pretend json instead object array of job objects called jobs. use following c# class definitions that;
public class jobswrapper { public list<job> jobs; } any construct used in json has c# equivalent, need decompose , determine is.
Comments
Post a Comment