83 lines
3.3 KiB
Nim
83 lines
3.3 KiB
Nim
# json-serialization
|
|
# Copyright (c) 2019-2023 Status Research & Development GmbH
|
|
# Licensed under either of
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
# at your option.
|
|
# This file may not be copied, modified, or distributed except according to
|
|
# those terms.
|
|
|
|
import
|
|
serialization/[formats, object_serialization]
|
|
|
|
export
|
|
formats
|
|
|
|
serializationFormat Json,
|
|
mimeType = "application/json"
|
|
|
|
template supports*(_: type Json, T: type): bool =
|
|
# The JSON format should support every type
|
|
true
|
|
|
|
type
|
|
EnumRepresentation* = enum
|
|
EnumAsString
|
|
EnumAsNumber
|
|
EnumAsStringifiedNumber
|
|
|
|
template flavorUsesAutomaticObjectSerialization*(T: type DefaultFlavor): bool = true
|
|
template flavorOmitsOptionalFields*(T: type DefaultFlavor): bool = true
|
|
template flavorRequiresAllFields*(T: type DefaultFlavor): bool = false
|
|
template flavorAllowsUnknownFields*(T: type DefaultFlavor): bool = false
|
|
template flavorSkipNullFields*(T: type DefaultFlavor): bool = false
|
|
|
|
var DefaultFlavorEnumRep {.compileTime.} = EnumAsString
|
|
template flavorEnumRep*(T: type DefaultFlavor): EnumRepresentation =
|
|
DefaultFlavorEnumRep
|
|
|
|
template flavorEnumRep*(T: type DefaultFlavor, rep: static[EnumRepresentation]) =
|
|
static:
|
|
DefaultFlavorEnumRep = rep
|
|
|
|
# If user choose to use `Json` instead of `DefaultFlavor`, it still goes to `DefaultFlavor`
|
|
template flavorEnumRep*(T: type Json, rep: static[EnumRepresentation]) =
|
|
static:
|
|
DefaultFlavorEnumRep = rep
|
|
|
|
# We create overloads of these traits to force the mixin treatment of the symbols
|
|
type DummyFlavor* = object
|
|
template flavorUsesAutomaticObjectSerialization*(T: type DummyFlavor): bool = true
|
|
template flavorOmitsOptionalFields*(T: type DummyFlavor): bool = false
|
|
template flavorRequiresAllFields*(T: type DummyFlavor): bool = false
|
|
template flavorAllowsUnknownFields*(T: type DummyFlavor): bool = false
|
|
template flavorSkipNullFields*(T: type DummyFlavor): bool = false
|
|
|
|
template createJsonFlavor*(FlavorName: untyped,
|
|
mimeTypeValue = "application/json",
|
|
automaticObjectSerialization = false,
|
|
requireAllFields = true,
|
|
omitOptionalFields = true,
|
|
allowUnknownFields = true,
|
|
skipNullFields = false) {.dirty.} =
|
|
type FlavorName* = object
|
|
|
|
template Reader*(T: type FlavorName): type = Reader(Json, FlavorName)
|
|
template Writer*(T: type FlavorName): type = Writer(Json, FlavorName)
|
|
template PreferredOutputType*(T: type FlavorName): type = string
|
|
template mimeType*(T: type FlavorName): string = mimeTypeValue
|
|
|
|
template flavorUsesAutomaticObjectSerialization*(T: type FlavorName): bool = automaticObjectSerialization
|
|
template flavorOmitsOptionalFields*(T: type FlavorName): bool = omitOptionalFields
|
|
template flavorRequiresAllFields*(T: type FlavorName): bool = requireAllFields
|
|
template flavorAllowsUnknownFields*(T: type FlavorName): bool = allowUnknownFields
|
|
template flavorSkipNullFields*(T: type FlavorName): bool = skipNullFields
|
|
|
|
var `FlavorName EnumRep` {.compileTime.} = EnumAsString
|
|
template flavorEnumRep*(T: type FlavorName): EnumRepresentation =
|
|
`FlavorName EnumRep`
|
|
|
|
template flavorEnumRep*(T: type FlavorName, rep: static[EnumRepresentation]) =
|
|
static:
|
|
`FlavorName EnumRep` = rep
|