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
|
|
|
|
|
2023-12-19 10:00:24 +00:00
|
|
|
template flavorUsesAutomaticObjectSerialization*(T: type DefaultFlavor): bool = true
|
|
|
|
template flavorOmitsOptionalFields*(T: type DefaultFlavor): bool = false
|
|
|
|
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
|
|
|
|
|
|
|
# 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
|