About Monkey 2 › Forums › Monkey 2 Code Library › JSON extensions
This topic contains 4 replies, has 3 voices, and was last updated by
nerobot 7 months, 2 weeks ago.
-
AuthorPosts
-
November 30, 2017 at 3:36 am #12023
Some JSON extensions I like to use.
DefValue allow us to assing-it-instead if desired key isn’t found.
FindValue method allow us to make nested search for objects by slash-separated key.
Monkey12345678910111213141516171819202122232425262728293031323334353637383940Class JsonObject ExtensionMethod GetBool:Bool( key:String,defValue:Bool )Local json:=SelfReturn json.Contains( key ) ? json[key].ToBool() Else defValueEndMethod GetString:String( key:String,defValue:String )Local json:=SelfReturn json.Contains( key ) ? json[key].ToString() Else defValueEndMethod GetInt:Int( key:String,defValue:Int )Local json:=SelfReturn json.Contains( key ) ? Int(json[key].ToNumber()) Else defValueEndMethod FindValue:JsonValue( key:String )key=key.Replace( "\","/" )Local keys:=key.Split( "/" )Local json:=Self.DataLocal jval:JsonValueFor Local k:=0 Until keys.LengthIf Not json.Contains( keys[k] ) Return Nulljval=json[ keys[k] ]If k=keys.Length-1 ExitIf Not jval.IsObject Return Nulljson=jval.ToObject()NextReturn jvalEndEndMonkey123456789101112Class JsonArray ExtensionFunction FromStrings:JsonArray( values:String[] )Local jvals:=New JsonValue[values.Length]For Local i:=0 Until values.Lengthjvals[i]=New JsonString( values[i] )NextReturn New JsonArray( jvals )EndEndAnd there is an example app:
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091Namespace myapp#Import "<std>"Using std..Const JSON:="{~qroot~q:{~qobjName1~q:~qTransformer~q,~qobjName2~q:~qWater~q,~qchild1~q:{~qsayWhat~q:~qHello, Monkey2!~q}}}"Function Main()Print JSONLocal json:=JsonObject.Parse( JSON )Local root:=json.GetObject( "root" )' this key exists:Print "1: "+root.GetString( "objName1","<1>" )' this key doesn't exists:Print "100500: "+root.GetString( "objName100500","<100500>" )' wow! searching for nested objects by single line of code' note: we use ?. operator here for case if json.FindValue()=Null' and ?Else operator for case if json.FindValue()?.ToString() is False' it may be incorrect if key exists but contains an empty line ""Print "request1: "+(json.FindValue( "root/child1/sayWhat" )?.ToString() ?Else "<not found>")' similar way but this key is incorrectPrint "request2: "+(json.FindValue( "root/child1/sayWhat/4" )?.ToString() ?Else "<not found>")EndClass JsonObject ExtensionMethod GetBool:Bool( key:String,defValue:Bool )Local json:=SelfReturn json.Contains( key ) ? json[key].ToBool() Else defValueEndMethod GetString:String( key:String,defValue:String )Local json:=SelfReturn json.Contains( key ) ? json[key].ToString() Else defValueEndMethod GetInt:Int( key:String,defValue:Int )Local json:=SelfReturn json.Contains( key ) ? Int(json[key].ToNumber()) Else defValueEndMethod FindValue:JsonValue( key:String )key=key.Replace( "\","/" )Local keys:=key.Split( "/" )Local json:=Self.DataLocal jval:JsonValueFor Local k:=0 Until keys.LengthIf Not json.Contains( keys[k] ) Return Nulljval=json[ keys[k] ]If k=keys.Length-1 ExitIf Not jval.IsObject Return Nulljson=jval.ToObject()NextReturn jvalEndEndClass JsonArray ExtensionFunction FromStrings:JsonArray( values:String[] )Local jvals:=New JsonValue[values.Length]For Local i:=0 Until values.Lengthjvals[i]=New JsonString( values[i] )NextReturn New JsonArray( jvals )EndEndNovember 30, 2017 at 7:34 am #12024Cool.
You’re a very productive coder. It’s nice having you around. You keep coding things that save time for other coders less experienced or versed in computer programming and I thank you for that.
Keep going, keep being productive. I appreciate all your efforts.
Regards,
November 30, 2017 at 7:39 am #12025July 25, 2018 at 9:42 pm #15155Never noticed this before, but awesome stuff! Thanks for the share Nerobot!
September 2, 2018 at 5:15 am #15363A new one to extract int-based enums:
Monkey123456Class JsonObject ExtensionMethod GetEnum<T>:T( key:String,defValue:T )Local json:=SelfReturn json.Contains( key ) ? Cast<T>(Int(json[key].ToNumber())) Else defValueEndEndExample
Monkey1234567Enum HoleFormsCircle,Square,DiamondEndLocal form:=json.GetEnum( "hole-form",HoleForms.Circle ) -
AuthorPosts
You must be logged in to reply to this topic.