2023-12-13 09:07:57 +00:00
|
|
|
# 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.
|
|
|
|
|
2021-03-19 02:13:55 +00:00
|
|
|
import
|
2023-12-18 04:05:12 +00:00
|
|
|
serialization/[formats, object_serialization]
|
|
|
|
|
|
|
|
export
|
|
|
|
formats
|
2021-03-19 02:13:55 +00:00
|
|
|
|
|
|
|
serializationFormat Json,
|
|
|
|
mimeType = "application/json"
|
|
|
|
|
|
|
|
template supports*(_: type Json, T: type): bool =
|
|
|
|
# The JSON format should support every type
|
|
|
|
true
|
|
|
|
|
2024-07-27 00:27:11 +00:00
|
|
|
type
|
|
|
|
EnumRepresentation* = enum
|
|
|
|
EnumAsString
|
|
|
|
EnumAsNumber
|
|
|
|
EnumAsStringifiedNumber
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
template flavorUsesAutomaticObjectSerialization*(T: type DefaultFlavor): bool = true
|
2024-01-17 00:48:42 +00:00
|
|
|
template flavorOmitsOptionalFields*(T: type DefaultFlavor): bool = true
|
2023-12-19 10:00:24 +00:00
|
|
|
template flavorRequiresAllFields*(T: type DefaultFlavor): bool = false
|
|
|
|
template flavorAllowsUnknownFields*(T: type DefaultFlavor): bool = false
|
2024-01-15 03:28:23 +00:00
|
|
|
template flavorSkipNullFields*(T: type DefaultFlavor): bool = false
|
2023-12-19 10:00:24 +00:00
|
|
|
|
2024-07-27 00:27:11 +00:00
|
|
|
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
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
# 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
|
2024-01-15 03:28:23 +00:00
|
|
|
template flavorSkipNullFields*(T: type DummyFlavor): bool = false
|
2023-12-19 10:00:24 +00:00
|
|
|
|
|
|
|
template createJsonFlavor*(FlavorName: untyped,
|
|
|
|
mimeTypeValue = "application/json",
|
|
|
|
automaticObjectSerialization = false,
|
|
|
|
requireAllFields = true,
|
|
|
|
omitOptionalFields = true,
|
2024-01-15 03:28:23 +00:00
|
|
|
allowUnknownFields = true,
|
|
|
|
skipNullFields = false) {.dirty.} =
|
2023-12-19 10:00:24 +00:00
|
|
|
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
|
2024-01-15 03:28:23 +00:00
|
|
|
template flavorSkipNullFields*(T: type FlavorName): bool = skipNullFields
|
2024-07-27 00:27:11 +00:00
|
|
|
|
|
|
|
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
|