Json Manual
Json Manual
Json Manual
ConsultingBVBA2006
JavaJSONTools
version1.2
BrunoRanschaert
Yourfeedbackisveryimportanttome.Youarewelcometosendmeyourremarks
orsuggestionsothatIcanimprovethelibrary.
mailto://[email protected]
1
Table of Contents
1.Introduction...................................................................................................................... ....3
1.1.Introduction...................................................................................................................3
1.2.Dependencies..............................................................................................................4
1.3.License...........................................................................................................................4
1.4.AboutS.D.IConsulting.................................................................................................4
1.5.ExtensionstotheJSONformat....................................................................................5
2.TheCoreTools............................................................................................................ ..........5
2.1.ParsingReadingJSON...............................................................................................5
2.2.RenderingWritingJSON............................................................................................5
2.3.JavaSerialization.........................................................................................................6
2.3.1.Primitivetypes........................................................................................................6
2.3.2.Referencetypes...................................................................................................7
2.4.Validation......................................................................................................................8
2.4.1.Basicrules...................................................................................................... .........9
2.4.1.1.type:true................................................................................................9
2.4.1.2.type:false..............................................................................................10
2.4.1.3.type:and................................................................................................10
2.4.1.4.type:or....................................................................................................10
2.4.1.5.type:not.................................................................................................10
2.4.2.Typerules.............................................................................................................10
2.4.2.1.type:complex,type:array,type:object,type:simple,
type:null,type:bool,type:string,type:number,type:int,
type:decimal.....................................................................................................11
2.4.3.Attributerules.......................................................................................................11
2.4.3.1.type:length..........................................................................................11
2.4.3.2.type:range..........................................................................................11
2.4.3.3.type:enum............................................................................................11
2.4.3.4.type:regexp..........................................................................................12
2.4.3.5.type:content.........................................................................................12
2.4.3.6.type:properties.....................................................................................12
2.4.4.Structuralrules.....................................................................................................13
2.4.4.1.type:ref.............................................................................................. ....13
2.4.4.2.type:let...............................................................................................13
1.1.1.1.type:custom............................................................................................14
1.1.1.2.type:switch.............................................................................................15
2.4.5.Example:Validatorforvalidators......................................................................15
3.ToolExtensions...................................................................................................................18
4.Futureextensions..............................................................................................................18
2
JavaJSONTools S.D.I.ConsultingBVBA2006
1. Introduction
1.1. Introduction
JSON(JavaScriptObjectNotation)isafileformattorepresentdata.Itissimilarto
XMLbuthasdifferentcharacteristics.Itissuitedtorepresentconfiguration
information,implementcommunicationprotocolsandsoon.XMLismoresuitedto
representannotateddocuments.JSONparsingisveryfast,theparsercanbekept
leanandmean.Itiseasyforhumanstoreadandwrite.Itisbasedonasubsetof
theJavaScriptProgrammingLanguage,StandardECMA2623rdEditionDecember
1999.JSONisatextformatthatiscompletelylanguageindependentbutuses
conventionsthatarefamiliartoprogrammersoftheCfamilyoflanguages,including
C,C++,C#,Java,JavaScript,Perl,Python,andmanyothers.Thesepropertiesmake
JSONanidealdatainterchangelanguage.Theformatisspecifiedon
http://www.json.org/,forthedetailspleasevisitthissite.
JSONisaverysimpleformat.Asaresult,theparsingandrenderingisfastandeasy,
youcanconcentrateonthecontentofthefileinsteadoftheformat.InXMLitis
oftendifficulttofullyunderstandallfeatures(e.g.namespaces,validation,...).Asa
result,XMLtendstobecomepartoftheproblemi.s.o.thesolution.InJSON
everythingiswelldefined,allaspectsoftherepresentationareclear,youcan
concentrateonhowyouaregoingtorepresentyourapplicationconcepts.
ThefollowingexamplecomesfromtheJSONexamplepage
http://www.json.org/example.html:
{
"widget" :
{
"debug" : "on",
"text" :
{
"onMouseUp" : "sun1.opacity = (sun1.opacity / 100) * 90;",
"hOffset" : 250,
"data" : "Click Here",
"alignment" : "center",
"style" : "bold",
"size" : 36,
"name" : "text1",
"vOffset" : 100
},
"image" :
{
"hOffset" : 250,
"alignment" : "center",
"src" : "Images/Sun.png",
"name" : "sun1",
"vOffset" : 250
},
"window" :
{
"width" : 500,
3
"height" : 500,
"title" : "Sample Konfabulator Widget",
"name" : "main_window"
}
}
}
ThisprojectwantstoprovidethetoolstomanipulateandusetheformatinaJava
application.
1.2. Dependencies
TheparserusesANTLR,sotheANTLRruntimeisneededforthis.Theprojectisbased
onthemaven2buildsystem.
TheJSONToolslibrariesarewrittenusingthenewlanguagefeaturesfromJDK1.5.
Enumerationsandgenericesareusedbecausethesemakethecodenicertoread.
Therearenodependenciestothenewlibraries.Ifyouwanttousethelibrariesforan
earlierversionoftheJDK,theretrotranslatortoolmightbeanoption
(http://retrotranslator.sourceforge.net).
1.3. License
ThelibraryisreleasedundertheLGPL.Youarefreetouseitforcommercialornon
commercialapplicationsaslongasyouleavethecopyrightintactandadda
referencetotheproject.Letmeknowwhatyoulikeandwhatyoudon'tlikeabout
thelibrarysothatIcanimproveit.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
4
JavaJSONTools S.D.I.ConsultingBVBA2006
Invokingtheparserisverysimple:
TheJSONmodelisahierarchyoftypes,thehierarchylookslikethis:
JSONValue
JSONComplex
JSONObject
JSONArray
JSONSimple
JSONNull
JSONBoolean
JSONString
JSONNumber
JSONInteger
JSonDecimal
Intheintroductionwealreadysawaprettyrenderingofsomewidgetdata.The
samestructurecanberenderedwithoutprettyprintinginordertoreduce
whitespace.Thiscanbeaninterestingfeaturewhenspaceoptimizationisvery
important,e.g.communicationprotocols.
5
{"widget":{"debug":"on","text":{"onMouseUp":"sun1.opacity = (sun1.opacity / 100) *
90;","hOffset":250,"data":"Click
Here","alignment":"center","style":"bold","size":36,"name":"text1","vOffset":100},"image"
:{"hOffset":250,"alignment":"center","src":"Images/Sun.png","name":"sun1","vOffset":250},
"window":{"width":500,"height":500,"title":"Sample Konfabulator
Widget","name":"main_window"}}}
Marshalling(convertingfromJavatoJSON)aswellasunmarshallingisvery
straightforward:
myTestObject = ...
Marshall marshall = new MarshallImpl();
JSONObject result = marshall.marshall(myTestObject);
Andtheotherwayaround:
YoumightwonderwhattheMarshallValueisallabout,whyisunmarshallinggiving
anextraobjectback?Theansweristhatwewenttogreatlengthstoprovide
marshalling/unmarshallingforbothJavareferencetypesasJavabasictypes.A
basictypeneedstobefetchedusingspecificmethods(thereisnootherway).In
ordertoprovidethesespecificmethodsweneedanextraclass.
The>attributewithvaluePindicatesaprimitivetype.The=attributecontains
therepresentationofthevalueandthetattributecontainstheoriginaljavatype.
6
JavaJSONTools S.D.I.ConsultingBVBA2006
{
">" : "A",
"c" : "I",
"=" :
[
{
">" : "P",
"=" : "0",
"t" : "int"
},
{
">" : "P",
"=" : "1",
"t" : "int"
},
{
">" : "P",
"=" : "2",
"t" : "int"
},
{
">" : "P",
"=" : "3",
"t" : "int"
},
{
">" : "P",
"=" : "4",
"t" : "int"
},
{
">" : "P",
"=" : "5",
"t" : "int"
}
]
}
Anobjectisrepresentedlikethis.
{
">" : "O",
"c" : "com.sdicons.json.serializer.MyBean",
"&" : "id0",
"=" :
{
"int2" :
{
">" : "null"
},
"ptr" :
{
">" : "R",
7
"*" : "id0"
},
"name" :
{
">" : "O",
"c" : "java.lang.String",
"&" : "id2",
"=" : "This is a test..."
},
"int1" :
{
">" : "null"
},
"id" :
{
">" : "O",
"c" : "java.lang.Integer",
"&" : "id1",
"=" : "1003"
}
}
}
The>markercontainsOforobjectthistime.TheCattributecontainsafully
qualifiedclassname.The&containsauniqueid,itcanbeusedtorefertothe
objectsothatweareabletorepresentrecursivedatastructures.The=attribute
containsaJSONobjecthavingapropertyforeachJavaBeanproperty.The
propertyvalueisrecursivelyarepresentationofaJavaobject.
Notethatthereisaspecialnotationtorepresentjavanullvalues.
{
">" : "null"
}
Alsonotethatyoucanrefertootherobjectswiththereferenceobjectwhichlooks
likethis:
{
">" : "R",
"*" : "id0"
}
2.4. Validation
ThistoolenablesyoutovalidateyourJSONfiles.Youcanspecifywhichcontentyou
expect,thevalidatorcanchecktheseconstraintsforyou.Thesystemis
straightforwardtouseandextend.Youcanaddyourownrulesifyouhavespecific
needs.ThevalidationdefinitionisinJSONasyouwouldexpect.
Builtinrules:
{
"name" : "Some rule name",
type : <built-in-type>
8
JavaJSONTools S.D.I.ConsultingBVBA2006
---
}
Avalidationdocumentconsistsofavalidationrule.Thisrulewillbeappliedtothe
JSONValuethathastobevalidated.Thevalidationrulescanbenested,soitis
possibletocreatecomplexrulesoutofsimplerones.Thetypeattributeis
obligatory.Thenameisoptional,itwillbeusedduringerrorreportingandforre
use.Thepredefinedrulesarelistedbelow.Thenamecancomeinhandywhile
debugging.Thenameofthefailingvalidationwillbeavailableintheexception.If
yougiveeachruleitsownnameornumber,youcanquicklyfindoutonwhich
predicatethevalidationfails.
Hereisanexampleofhowyoucancreateavalidator.
Andnowthatyouhavethevalidator,youcanstartvalidatingyourdata.
// Now we can use the validtor to check on our data. We can test if the data has the
// correct format or not.
lValidator.validate(lMyData);
{
"name" :"This validator validates *everything*",
"type" :"true"
}
9
2.4.1.2. type:false
Parameters:
Description:Thisrulealwaysfails.
Example:Avalidatorthatrejectsalldatastructures.
{
"name" :"This validator rejects all",
"type" :"false"
}
2.4.1.3. type:and
Parameters:
rules:arrayofnestedrules.
Description:Allnestedruleshavetoholdfortheandruletosucceed.
Example:Avalidatorthatsucceedsiftheobjectunderscrutinyisbothalistandhas
contentconsistingofintegers.
{
"name" :"List of integers",
"type" :"and",
"rules" : [{"type":"array"},{"type":"content","rule":{"type":"int"}}]
}
2.4.1.4. type:or
Parameters:
rules:arrayofnestedrules.
Description:Oneofthenestedruleshastosucceedforthisruletosucceed.
Example:Avalidatorthatvalidatesnullorintegers.
{
"name" :"Null or int",
"type" :"or",
"rules" : [{"type":"int"},{"type":"bool"}]
}
2.4.1.5. type:not
Parameters:
rule:Asinglenestedrule.
Description:Therulesucceedsifthenestedrulefailsandviceversa.
10
JavaJSONTools S.D.I.ConsultingBVBA2006
{
"name" :"Array of length 5",
"type" :"and",
"rules" : [{"type":"array"}, {"type":"length","min":5,"max":5}]
}
{
"name" :"Range validator",
"type" :"range",
"min" : 50,
"max" : 100
}
2.4.3.3. type:enum
Parameters:
11
values:AnarrayofJSONvalues.
Description:Thevaluehastooccurintheprovidedlist.Thelistcancontainsimple
typesaswellascomplexnestedtypes.
Example:Anenumvalidator.
{
"name" :"Enum validator",
"type" :"enum",
"values" : [13, 17, "JSON", 123.12, [1, 2, 3], {"key":"value"}]
}
2.4.3.4. type:regexp
Parameters:
pattern:Aregexppattern.
Description:Forstrings,requiresapredefinedformataccordingtotheregular
expression.
Example:Avalidatorthatvalidatesstringscontainingasequenceofa's,b'sandc's.
{
"name" :"A-B-C validator",
"type" :"regexp",
"pattern" : "a*b*c*"
}
2.4.3.5. type:content
Parameters:
rule:Therulethatspecifieshowthecontentofacomplexstructurean
arrayorthepropertyvaluesofanobjectshouldbehave.
Description:Notethatincontrastwiththepropertiesrule(forobjects),youcan
specifyinasinglerulewhatallpropertyvaluesofanobjectshouldlooklike.
Example:Seetype:and.
2.4.3.6. type:properties
Parameters:
pairs:Alistofkey/valuepairdescriptions.Eachdescriptioncontainsthree
properties:keythekeystring;optionalabooleanindicatingwhetherthis
propertyisoptionalornotandruleavalidationrulethatshouldbeapplied
tothepropertiesvalue.Notethatincontrastwitthecontentruleaboveyou
canspecifyaruleperattribute.
Description:Thispredicateisonlyapplicable(andonlyhasmeaning)onobjectdata
12
JavaJSONTools S.D.I.ConsultingBVBA2006
structures.Itwillfailonanyothertype.
Example:
{
"name" :"Contact spec.",
"type" :"properties",
"pairs" : [{"key":"name", "optional":false, "rule":{"type":"string"}},
{"key":"country", "optional":false, "rule":{"type":"string"}},
{"key":"salary", "optional":true, "rule":{"type":"decimal"}}
]
}
Itwillvalidateobjectslookinglikethis:
2.4.4.1. type:ref
Parameters:
*:Thenameoftheruletoinvoke.
Description:Thisruleletsyouspecifyrecursiverules.Becarefulnottocreateinfinite
validationswhichisquitepossibleusingthisrule.Thecontainingrulewillbefetched
justbeforevalidation,therewillbenoerrormessageduringconstructionwhenthe
containingruleisnotfound.Therulewillfailinthiscase.Ifthereareseveralruleswith
thesamename,onlythelastonewiththatnameisrememberedandthelastone
willbeused.
Example:Avalidatorthatvalidatesnestedlistsofintegers.Arefisneededtoenable
recursioninthevalidator.
{
"name" :"Nested list of integers",
"type" :"and",
"rules" :
[
{"type":"array"},
{"type":"content",
"rule":
{"type" : "or",
"rules":
[{"type":"int"},
{"type":"ref", "*" : "Nested list of integers"}]
}}
]
}
13
Description:Letsyouspecifyanumberofnamedrulesinadvance.Itisa
conveniencerulethatletsyouspecifyalistofglobalsharedvalidationrulesin
advancebeforeusingtheselateron.Itbecomespossibletofirstdefineanumberof
recurringtypesandthengivethestartingpoint.Itisautilityrulethatletsyoutackle
morecomplexvalidations.Notethatitmakesnosensetodefineanonymousrules
insidethelist,itisimpossibletorefertotheselateron.
Example:
{
"name" :"Let test - a's or b's",
"type" :"let",
"*" : "start",
"rules" :
[{"name":"start", "type":"or", "rules":[{"type":"ref", "*":"a"},
{"type":"ref", "*":"b"}]},
{"name":"a", "type":"regexp", "pattern":"a*"},
{"name":"b", "type":"regexp", "pattern":"b*"}
]
}
1.1.1.1. type:custom
Parameters:
class:Thefullyqualifiedclassnameofthevalidator.
Description:Aninstanceofthisvalidatorwillbecreatedandwillbegivenahash
mapofvalidations.Acustomvalidatorshouldbederivedfrom
com.sdicons.json.validator.impl.predicates.CustomValidator.
Example:
{
"name" :"Custom test",
"type" :"custom",
"class" : "com.sdicons.json.validator.MyValidator"
}
Thevalidatorclasslookslikethis:
14
JavaJSONTools S.D.I.ConsultingBVBA2006
1.1.1.2. type:switch
Parameters:
key:Thekeynameoftheobjectthatwillactasthediscriminator.
case:Alistofobjectscontainingtheparametersvaluesandrule.The
firstoneisalistofvaluesthesecondoneavalidatorrule.
Description:Theswitchvalidatorisaconvenienceone.Itisasubsetoftheor
validator,buttheproblemwiththeorvalidatoristhatitdoesabadjobforerror
reportingwhenthingsgowrong.Thereasonisthatallrulesfailanditisnotalways
clearwhy,becausethereasonarulefailsmightbesomelevelsdeeper.Theswitch
validatorselectsavalidatorbasedonthevalueofapropertyencounteredinthe
valuebeingvalidated.Theerrorproducedwillbetheoneoftheselectedvalidator.
Thefirstapplicablevalidatorisused,thefollowingonesareignored.
Example:Thetoplevelruleinthevalidatorforvalidatorscontainsaswitchthatcould
havebeendescribedbyanor,buttheswitchgivesbettererrormessages.
{
"name":"Validator validator",
"type":"let",
"*":"rule",
"rules":
[
########## START ##########
{
"name":"rule",
"type":"switch",
"key":"type",
"case":
[
{"values":["true", "false", "null"], "rule":{"type":"ref","*":"atom-rule"}},
{"values":["int", "complex", "array", "object", "simple",
"null", "bool", "string", "number", "decimal"],
"rule":{"type":"ref","*":"type-rule"}},
{"values":["not", "content"], "rule":{"type":"ref","*":"rules-rule"}},
{"values":["and", "or"], "rule":{"type":"ref","*":"ruleset-rule"}},
{"values":["length", "range"], "rule":{"type":"ref","*":"minmax-rule"}},
{"values":["ref"], "rule":{"type":"ref","*":"ref-rule"}},
{"values":["custom"], "rule":{"type":"ref","*":"custom-rule"}},
{"values":["enum"], "rule":{"type":"ref","*":"enum-rule"}},
{"values":["let"], "rule":{"type":"ref","*":"let-rule"}},
{"values":["regexp"], "rule":{"type":"ref","*":"regexp-rule"}},
{"values":["properties"], "rule":{"type":"ref","*":"properties-rule"}},
{"values":["switch"], "rule": {"type":"ref","*":"switch-rule"}}
]
},
########## RULESET ##########
{
"name":"ruleset",
"type":"and",
15
"rules":[{"type":"array"},{"type":"content","rule":{"type":"ref","*":"rule"}}]
},
########## PAIRS ##########
{
"name":"pairs",
"type":"and",
"rules":[{"type":"array"},{"type":"content","rule":{"type":"ref","*":"pair"}}]
},
########## PAIR ##########
{
"name":"pair",
"type":"properties",
"pairs" :
[{"key":"key", "optional":false, "rule":{"type":"string"}},
{"key":"optional", "optional":false, "rule":{"type":"bool"}},
{"key":"rule", "optional":false, "rule":{"type":"ref","*":"rule"}}
]
},
########## CASES ##########
{
"name":"cases",
"type":"and",
"rules":[{"type":"array"},{"type":"content","rule":{"type":"ref","*":"case"}}]
},
########## CASE ##########
{
"name":"case",
"type":"properties",
"pairs" :
[{"key":"values", "optional":false, "rule":{"type":"array"}},
{"key":"rule", "optional":false, "rule":{"type":"ref","*":"rule"}}
]
},
########## ATOM ##########
{
"name":"atom-rule",
"type":"properties",
"pairs" :
[{"key":"name", "optional":true, "rule":{"type":"string"}},
{"key":"type", "optional":false, "rule":
{"type":"enum","values":["true", "false", "null"]}}
]
},
########## RULESET-RULE ##########
{
"name":"ruleset-rule",
"type":"properties",
"pairs" :
[{"key":"name", "optional":true, "rule":{"type":"string"}},
{"key":"type", "optional":false, "rule":{"type":"enum","values":["and", "or"]}},
{"key":"rules", "optional":false, "rule":{"type":"ref","*":"ruleset"}}
]
},
########## RULES-RULE ##########
{
"name":"rules-rule",
"type":"properties",
"pairs" :
[{"key":"name", "optional":true, "rule":{"type":"string"}},
{"key":"type", "optional":false, "rule":{"type":"enum","values":["not", "content"]}},
{"key":"rule", "optional":false, "rule":{"type":"ref","*":"rule"}}
]
},
########## TYPE ##########
{
16
JavaJSONTools S.D.I.ConsultingBVBA2006
"name":"type-rule",
"type":"properties",
"pairs" :
[{"key":"name", "optional":true, "rule":{"type":"string"}},
{"key":"type", "optional":false, "rule":{"type":"enum",
"values":["int", "complex", "array", "object",
"simple", "null", "bool", "string", "number",
"decimal"]}}
]
},
########## MINMAX ##########
{
"name":"minmax-rule",
"type":"properties",
"pairs" :
[{"key":"name", "optional":true, "rule":{"type":"string"}},
{"key":"type", "optional":false, "rule":{"type":"enum","values":["length", "range"]}},
{"key":"min", "optional":true, "rule":{"type":"number"}},
{"key":"max", "optional":true, "rule":{"type":"number"}}
]
},
########## REF ##########
{
"name":"ref-rule",
"type":"properties",
"pairs" :
[{"key":"name", "optional":true, "rule":{"type":"string"}},
{"key":"type", "optional":false, "rule":{"type":"enum","values":["ref"]}},
{"key":"*", "optional":false, "rule":{"type":"string"}}
]
},
########## CUSTOM ##########
{
"name":"custom-rule",
"type":"properties",
"pairs" :
[{"key":"name", "optional":true, "rule":{"type":"string"}},
{"key":"type", "optional":false, "rule":{"type":"enum","values":["custom"]}},
{"key":"class", "optional":true, "rule":{"type":"string"}}
]
},
########## ENUM ##########
{
"name":"enum-rule",
"type":"properties",
"pairs" :
[{"key":"name", "optional":true, "rule":{"type":"string"}},
{"key":"type", "optional":false, "rule":{"type":"enum","values":["enum"]}},
{"key":"values", "optional":true, "rule":{"type":"array"}}
]
},
########## LET ##########
{
"name":"let-rule",
"type":"properties",
"pairs" :
[{"key":"name", "optional":true, "rule":{"type":"string"}},
{"key":"type", "optional":false, "rule":{"type":"enum","values":["let"]}},
{"key":"rules", "optional":false, "rule":{"type":"ref","*":"ruleset"}},
{"key":"*", "optional":false, "rule":{"type":"string"}}
]
},
########## REGEXP ##########
{
"name":"regexp-rule",
17
"type":"properties",
"pairs" :
[{"key":"name", "optional":true, "rule":{"type":"string"}},
{"key":"type", "optional":false, "rule":{"type":"enum","values":["regexp"]}},
{"key":"pattern", "optional":false, "rule":{"type":"string"}}
]
},
########## PROPERTIES ##########
{
"name":"properties-rule",
"type":"properties",
"pairs" :
[{"key":"name", "optional":true, "rule":{"type":"string"}},
{"key":"type", "optional":false, "rule":{"type":"enum","values":["properties"]}},
{"key":"pairs", "optional":false, "rule":{"type":"ref","*":"pairs"}}
]
},
########## SWITCH ##########
{
"name":"switch-rule",
"type":"properties",
"pairs" :
[{"key":"name", "optional":true, "rule":{"type":"string"}},
{"key":"type", "optional":false, "rule":{"type":"enum","values":["switch"]}},
{"key":"key", "optional":false, "rule":{"type":"string"}},
{"key":"case", "optional":false, "rule":{"type":"ref","*":"cases"}}
]
}
]
}
3. Tool Extensions
3.1. Log4J
Isinthejsontoolslog4jlibrary.ItcontainsaLog4Jlayoutthatwillformatthemessages
inJSONformat.Incombinationwiththeexistingappendersthismakesitpossibleto
createJSONoutputwhichwillbeeasytoparseusingthecoretools.
4. Future extensions
TherearemanyotherapplicationswhichcouldbenefitfromtheJSONformat.Itwill
dependonthefeedbackIreceiveandmyownneedswhichfeatureswillbe
implementedfirst.Herearesomecandidates:
Springconfigurationfile.
Hibernatemappingfiles.InasubstantialapplicationthecurrentXMLfilestakeages
toparseandtoload.ThisprocesscouldbespedupbyusingtheJSONformat.
18