const mixinafJson::JsonConverters
afJson::JsonConverters
@Js
(Service) - Converts Fantom objects to and from their JSON representation.
- defConvs
static Type:JsonConverter defConvs()The default set of JSON <-> Fantom converters.
- fromJson
abstract Obj? fromJson(Str? json, Type fantomType)Converts a JSON string to the given Fantom type.
- fromJsonObj
abstract Obj? fromJsonObj([Str:Obj?]? jsonObj, Type fantomType)Converts a JSON object to the given Fantom type.
jsonObjis nullable so converters can choose whether or not to create empty lists and maps.- fromJsonVal
abstract Obj? fromJsonVal(Obj? jsonVal, Type? fantomType)Converts a JSON value to the given Fantom type. If
fantomTypeisnullthennullis always returned.jsonValis nullable so converters can choose whether or not to create empty lists and maps.- get
@Operator
abstract JsonConverter get(Type type)Returns the
Converterinstance used to convert the given type.- make
static new make([Type:JsonConverter]? converters := null, [Str:Obj?]? options := null)Returns a new
JsonConvertersinstance.If
convertersisnullthendefConvsis used. Common options are:afJson.makeEntity : |Type type, Field:Obj? fieldVals->Obj?| { BeanBuilder.build(type, vals) } afJson.strictMode : false afJson.dateFormat : "YYYY-MM-DD" afJson.dateTimeFormat : "YYYY-MM-DD'T'hh:mm:ss.FFFz" afJson.propertyCache : JsonPropertyCache()Override
makeEntityto have IoC create entity instances. SetstrictModetotrueto Err if the JSON contains unmapped data.- normaliseDotNetKeyNames
static |Obj?->Obj? normaliseDotNetKeyNames()Returns a fn that normalises .NET JSON key names into standard Fantom camelCase names.
Use as a hook option:
converters := JsonConverters(null, [ "afJson.fromJsonHook" : JsonConverters.normaliseDotNetKeyNames ])- toJson
abstract Str toJson(Obj? fantomObj, Obj? options := null)Converts the given Fantom object to its JSON string representation.
optionsis passed toJsonWriter, so may just betruefor pretty printing.- toJsonObj
abstract [Str:Obj?]? toJsonObj(Obj? fantomObj)Converts the given Fantom object to its JSON object representation.
fantomObjis nullable so converters can create empty / default objects.- toJsonVal
abstract Obj? toJsonVal(Obj? fantomObj, Type? fantomType)Converts the given Fantom object to its JSON representation. If
fantomTypeisnullthennullis always returned.fantomTypeis required in casefantomObjis null.fantomObjis nullable so converters can create empty / default objects.- withOptions
abstract JsonConverters withOptions(Str:Obj? newOptions)Returns a new
JsonConverterswhose options are overridden with the given ones.