const mixinafJson::JsonConverters

afJson::JsonConverters

@Js

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

defConvs

Source

const 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. If fantomType is null, then null is always returned.

fromJsonObj

Source

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

Converts a JSON object to the given Fantom type.

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

fromJsonVal

Source

abstract Obj? fromJsonVal(Obj? jsonVal, Type? fantomType)

Converts a JSON value to the given Fantom type. If fantomType is null then null is always returned.

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. 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()
afJson.serializableMode  : true

Override makeEntity to have IoC create entity instances. Set strictMode to true to Err if the JSON contains unmapped data.

Serializable 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

const 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, [
    "afJson.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.

toJsonObj

Source

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

Converts the given Fantom object to its JSON object representation.

fantomObj is nullable so converters can create empty / default objects.

toJsonVal

Source

abstract Obj? toJsonVal(Obj? fantomObj, Type? fantomType)

Converts the given Fantom object to its JSON representation. If fantomType is null then null is always returned.

fantomType is required in case fantomObj is null. fantomObj is nullable so converters can create empty / default objects.

withOptions

Source

abstract JsonConverters withOptions(Str:Obj? newOptions)

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