const mixinafJson::JsonConverters

afJson::JsonConverters

@Js

(Service) - Converts Fantom objects to and from their JSON representation.

defConvs

Source

static Type:JsonConverter defConvs()

The default set of JSON <-> Fantom converters.

fromJson

Source

abstract Obj? fromJson(Str? json, Type? fantomType)

Converts a JSON string to the given Fantom type.

Returns null if json is null.

fromJsonArray

Source

abstract Obj?[]? fromJsonArray(Obj?[]? jsonArray, Type? fantomValType := null)

Converts a list of JSON values to the given Fantom (non-list) type.

fromJsonList(list, MyEntity#)

Convenience for calling fromJsonVal() with a cast.

fromJsonObj

Source

abstract Obj? fromJsonObj([Str:Obj?]? jsonObj, Type? fantomType := null)

Converts a JSON object to the given Fantom type.

Convenience for calling fromJsonVal() with a cast.

If fantomType is null then the obj is inspected for a _type property.

fromJsonVal

Source

abstract Obj? fromJsonVal(Obj? jsonVal, Type? fantomType := null)

Converts a JSON value to the given Fantom type.

If fantomType is null then the obj is inspected for a _type property, else a reasonable guess is made (and the option docToTypeFn is then called as a last resort.)

jsonVal is nullable so converters can choose whether or not to create empty lists and maps.

get

Source

@Operator
abstract JsonConverter get(Type type)

Returns the Converter instance used to convert the given type.

make

Source

static new make([Type:JsonConverter]? converters := null, [Str:Obj?]? options := null)

Returns a new JsonConverters instance.

If converters is null then defConvs is used. Some defaults are:

makeEntityFn      : |Type type, Field:Obj? fieldVals->Obj?| { BeanBuilder.build(type, vals) }
makeJsonObjFn     : |-> Str:Obj?| { Str:Obj?[:] { ordered = true } }
fromJsonHookFn    : |Obj? obj, JsonConverterCtx->Obj?| { obj }
toJsonHookFn      : |Obj? obj, JsonConverterCtx->Obj?| { obj } 
dateFormat        : "YYYY-MM-DD"
dateTimeFormat    : "YYYY-MM-DD'T'hh:mm:ss.FFFz"
strictMode        : false
propertyCache     : JsonPropertyCache()
pickleMode        : false
doNotWriteNulls   : false
encodeDecodeUris  : true - set to false to use Uri.toStr / Str.toUri

Override makeEntityFn to have IoC create entity instances.

Hook fns are called before conversion takes place.

Date formats are used to serialise Date and Time objects.

Set strictMode to true to Err if the JSON contains unmapped data.

Pickle Mode is where all non @Transient fields are converted, regardless of any @JsonProperty facets. Data from @JsonProperty facets, however, is still honoured if defined.

normaliseKeyNamesFn

Source

static |Obj?->Obj? normaliseKeyNamesFn()

Returns a fn that normalises .NET and snake_case key names into standard Fantom camelCase names.

.NET examples
-------------
UniqueID        -->  uniqueId
SWVersion       -->  swVersion
MegaVERIndex    -->  megaVerIndex
UtilITEMS.Rec   -->  utilItems.rec

Snake_case examples
-------------------
unique_id       -->  uniqueId
sw_Version      -->  swVersion
mega_VER_Index  -->  megaVerIndex

Use as a hook option:

converters := JsonConverters(null, [
    "fromJsonHook" : JsonConverters.normaliseKeyNamesFn
])
toJson

Source

abstract Str toJson(Obj? fantomObj, Obj? options := null)

Converts the given Fantom object to its JSON string representation.

options is passed to JsonWriter, so may just be true for pretty printing.

toJsonArray

Source

abstract Obj?[]? toJsonArray(Obj?[]? fantomList)

Deeply converts the given Fantom List to its JSON representation.

Convenience for calling toJsonVal() with a cast.

toJsonObj

Source

abstract [Str:Obj?]? toJsonObj(Obj? fantomObj)

Converts the given Fantom object to its JSON object representation.

Convenience for calling toJsonVal() with a cast.

toJsonVal

Source

abstract Obj? toJsonVal(Obj? fantomObj, Type? fantomType := null)

Converts the given Fantom object to its JSON representation.

fantomObj is nullable so converters can create empty / default objects. fantomType in case fantomObj is null, but defaults to fantomObj?.typeof.

withOptions

Source

abstract JsonConverters withOptions(Str:Obj? newOptions)

Returns a new JsonConverters whose options are overridden with the given ones.