Comments (3)
See the docs on variant object types, specifically this:
The value of a discriminator field must be loaded before any value of a field that depends on it. Therefore, a YAML mapping cannot be used to serialize variant object types - the YAML specification explicitly states that the order of key-value pairs in a mapping must not be used to convey content information. So, any variant object type is serialized as a list of key-value pairs.
This should answer 1.
Concerning 2, multiple case
constructs in an object are currently not supported. I am not entirely sure why, since it has been a long time since I wrote that code. I can't promise a timely fix but suggest you have a look at {.implicit.}
and check if that can solve your problem. Your example would become:
import yaml/serialization, streams
type
DataType = enum
Default
Sequence
Inner1 {.implicit.} = object
case kind: DataType
of Default: value: string
of Sequence: valueSeq: seq[string]
Inner2 {.implicit.} = object
case kind : DataType
of Default: value: string
of Sequence: valueSeq: seq[string]
Data = object
data: string
inner1: Inner1
inner2: Inner2
setTag(seq[string], "!list".Tag)
var node: Data
let s = newFileStream("in.yaml")
load(s, node)
s.close()
This can load the following YAML:
data: test
inner1: test # sets kind to Default and value to "test"
inner2: !list [test2, test3] # sets kind to Sequence.
from nimyaml.
I gave incorrect variable naming in the examples. I don't need variant object types. I need variant object fields and these variants may be several.
type
DataType = enum
Default
Type1
Type2
Config = object
name: string
case kind: DataType
of Type1:
param1: string
param2: string
of Type2:
param3: string
param4: seq[string]
else: discard
name: some_confing
kind: Type2
param3: str
param4:
- str1
- str2
from nimyaml.
That Config
type is loadable but you must put the fields in a sequence for the reason quoted above from the docs. It's either that or use an implicit type and wrap the inner params into objects.
from nimyaml.
Related Issues (20)
- loadDom SIGSEGV: Illegal storage access. HOT 7
- [Documentation] Simple example for loading a simple .yml file? HOT 3
- Compilation hint: 'dump' cannot raise 'YamlSerializationError' [XCannotRaiseY] HOT 5
- dumping leaves part of string on same line as >- making the file unable to be loaded from HOT 8
- `| >` wrapping functionality can generate invalid YAML HOT 1
- Fails to parse a triple dot in string lit
- defaultVal is not observed on stropped names HOT 2
- Anchor in ignored tag HOT 5
- Feature: Let user choose/disable maximum line length for presentation HOT 4
- BUG: loading zero to unsigned integer generates a YamlConstructionError
- BUG: loading to float32 generates an error HOT 3
- Can't get optional fields to work HOT 3
- ignore pragma doesn't work on nested maps HOT 1
- nimble install yaml fails on devel version of Nim (needs new release) HOT 1
- Crashes on incorrect YAML HOT 2
- Raise an unlisted exception HOT 13
- Parent fields cause failure when deserializing child object HOT 3
- Catchable exception warnings HOT 2
- Seemingly pointless proc HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nimyaml.