Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YAML anchors don't seem to work with @JsonCreator #22

Open
ryegleason opened this issue May 30, 2017 · 0 comments
Open

YAML anchors don't seem to work with @JsonCreator #22

ryegleason opened this issue May 30, 2017 · 0 comments
Labels
yaml Issue related to YAML format backend

Comments

@ryegleason
Copy link

Java code:

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class)
class Student {
	private String name;
	private Integer age;

	@JsonCreator
        public Student(@JsonProperty("age") int age, @JsonProperty(value = "name") String name){
		this.name = name;
		this.age = age;
	}

        public String toString(){
            return "Student [ name: "+name+", age: "+ age+ " ]";
    }
}

public static void main(String args[]){
        try {
            YAMLMapper mapper = new YAMLMapper(new YAMLFactory());

            Student student = mapper.readValue(new File("student_yaml.yml"), Student.class);

	        System.out.println(student);
        }
        catch (Exception e) {
	        e.printStackTrace();
        }
    }

student_yaml.yml:

---
&1 name: "billy"
age: 5

Runtime Error:

com.fasterxml.jackson.databind.JsonMappingException: No Object Id found for an instance of Student, to assign to property '@id'
 at [Source: student_yaml.yml; line: 3, column: 7]
	at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
	at com.fasterxml.jackson.databind.DeserializationContext.reportUnresolvedObjectId(DeserializationContext.java:1259)
	at com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer.handleIdValue(PropertyValueBuffer.java:241)
	at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:143)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:471)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1198)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:314)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithObjectId(BeanDeserializerBase.java:1168)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:146)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2740)
	at JacksonTester.main(JacksonTester.java:50)

So for this, when attempting to use the YAML anchor notation, it doesn't seem to be recognized. Everything works fine when I make Student stop using JsonCreator (and make the fields public, of course), or change student_yaml.yml to

---
'@id': 1
name: "billy"
age: 5
@cowtowncoder cowtowncoder added the yaml Issue related to YAML format backend label Jun 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
yaml Issue related to YAML format backend
Projects
None yet
Development

No branches or pull requests

2 participants