@JsonIdentityInfo(generator = ObjectIdGenerators.None.class)
class YamlScratch {
@JsonProperty("misc")
List<List<String>> misc;
@JsonProperty("contents")
Map<String, Config> contents;
static class Config {
@JsonProperty("header")
String header;
@JsonProperty("labels")
@JsonIdentityInfo(generator = ObjectIdGenerators.None.class)
List<String> labels;
}
}
misc:
- &letters
- Aie
- Bee
- See
- &numbers
- One
- Two
- Three
contents:
letters:
header: "This is a list of phonetic letters"
labels: *letters
numbers:
header: "This is a list of number spellings"
labels: *numbers
moreletters:
header: "this is another copy of the letters"
labels: *letters
$ python3
>>> import yaml
>>> f = open("/Users/johnpassaro/Library/Preferences/IdeaIC2018.3/scratches/scratch.yml")
>>> y = yaml.load(f)
>>> y
{'misc': [['Aie', 'Bee', 'See'], ['One', 'Two', 'Three']], 'contents': {'letters': {'header': 'This is a list of phonetic letters', 'labels': ['Aie', 'Bee', 'See']}, 'numbers': {'header': 'This is a list of number spellings', 'labels': ['One', 'Two', 'Three']}}}
List<String> labels;
Object labels;
YAMLParser.Feature.USE_NATIVE_OBJECT_ID
. That would seem to be exactly what I need, but that enum value is not present in 2.9.new ObjectMapper(new YAMLFactory()).readValue(new File("/path/to/myfile.yaml"), YamlScratch.class);
On Tue, Feb 12, 2019, 23:02 Tatu Saloranta wrote:
On Tue, Feb 12, 2019 at 1:18 PM John Passaro wrote:
>
> Hello folks! I am trying to deserialize a yaml file with anchors and references. There are some existing StackOverflow questions along these lines but the answers aren't quite getting me to the finish line.
>
> [...]
>
>
> The idea is that I have lists that may be referenced more than once in the "contents" tree, and I'd like to be able to reference them concisely.
>
> [...]
>
> With Jackson (2.9), I get an error:
>
> Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.ArrayList out of VALUE_STRING token
> at [Source: (File); line: 14, column: 13] (through reference chain: YamlScratch["contents"]->java.util.LinkedHashMap["letters"]->YamlScratch$Config["labels"])
>
> [...]
@JsonIdentityInfo(generator = ObjectIdGenerators.None.class)
class ScratchModel {
@JsonProperty("contents")
@JsonIdentityInfo(generator = ObjectIdGenerators.None.class)
Map<String, Config> contents;
@JsonProperty("misc")
@JsonIdentityInfo(generator = ObjectIdGenerators.None.class)
Misc misc;
@JsonIdentityInfo(generator = ObjectIdGenerators.None.class)
static class Config {
@JsonProperty("header")
String header;
@JsonProperty("labels")
@JsonIdentityInfo(generator = ObjectIdGenerators.None.class)
List<String> labels;
}
@JsonIdentityInfo(generator = ObjectIdGenerators.None.class)
private static class Misc {
@JsonCreator
Misc(List<MiscInner> inners) { }
}
@JsonIdentityInfo(generator = ObjectIdGenerators.None.class)
private static class MiscInner {
@JsonCreator
MiscInner(List<String> strings) { }
}
}
--
You received this message because you are subscribed to a topic in the Google Groups "jackson-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jackson-user/b3ucK2emRbE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
class YamlScratch {
private static final String YAML_CONTENT = "" +
"foo: &foo\n" +
" value: bar\n" +
"boo: *foo\n";
public static void main(String[] args) throws Exception {
ScratchModel yamlScratch = new ObjectMapper(
new YAMLFactory()
).readValue(YAML_CONTENT, ScratchModel.class);
System.out.printf("foo = %s, boo=%s%n", yamlScratch.foo, yamlScratch.boo);
}
@JsonIdentityInfo(generator = ObjectIdGenerators.None.class)
public static class ScratchModel {
@JsonProperty("foo")
StringHolder foo;
@JsonProperty("boo")
StringHolder boo;
}
@JsonIdentityInfo(generator = ObjectIdGenerators.None.class)
public static class StringHolder {
@JsonProperty("value")
String value;
@Override
public String toString() {
return value;
}
}
}
Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `YamlScratch$StringHolder` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('foo')
at [Source: (StringReader); line: 3, column: 6] (through reference chain: YamlScratch$ScratchModel["boo"])
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1329)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1031)
at com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:370)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:314)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1351)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:170)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161)
at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:136)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:287)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2992)
at YamlScratch.main(scratch_8.java:17)
You received this message because you are subscribed to a topic in the Google Groups "jackson-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jackson-user/b3ucK2emRbE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jackson-user...@googlegroups.com.