Comments (5)
This is working as expected. When parsing JSON with a top-level _type attribute, you have to tell the parser the possible classes it could be through the use of a bean dictionary.
public class Test {
public static void main(String[] args) {
Foo foo = new Foo();
JsonSerializer js = JsonSerializer.create().addRootType().build();
String json = js.serialize(foo);
System.err.println(json); // {"_type":"foo","bar":"x"}
JsonParser jp = JsonParser.create().beanDictionary(Foo.class).build();
Object o = jp.parse(json, Object.class);
System.err.println(o.getClass().getName()); // Test$Foo
}
@Bean(typeName="foo")
public static class Foo {
public String bar = "x";
}
}
The reason for this design:
- The only way to otherwise automatically support your scenario would require scanning the entire classpath looking for classes with @bean(typeName) annotations in order to build a name/type map. That would be very expensive.
- Allowing you to specify any type name would be a large deserialization security hole.
See the following documentation on bean dictionaries:
https://juneau.apache.org/site/apidocs-9.0.1/index.html#jm.BeanDictionaries
from juneau.
Thank you,
I figured as much (the call to beanDictionary).
The challenge I have is that I needed to parse library classes that I don't own, and can't change, and are not annotated.
When I attempted to use dynamic annotations (via the annotations() API methods) that didn't work. In other words, the beanDictionary() and annotations() features do not compose.
I ended up having to implement a mixin like mechanism (similar to Jackson's) which modifies class and/or method level declared annotation metadata via reflection. This is fragile since it depends on JDK implementation details, and also the semantics of the (default) annotations provider in Juneau (that loads the declared annotations).
To summarize. It seem parsing does not support polymorphic parsing with types that do not statically declared annotations.
from juneau.
@jonathanshraga - This appeared to work for me:
package org.apache.juneau;
import org.apache.juneau.annotation.*;
import org.apache.juneau.json.*;
public class Test {
public static void main(String[] args) {
Foo foo = new Foo();
JsonSerializer js = JsonSerializer
.create()
.annotations(BeanAnnotation.create(Foo.class).typeName("foo").build())
.addRootType()
.build();
String json = js.serialize(foo); // Prints {"_type":"foo","bar":"x"}
System.err.println(json);
JsonParser jp = JsonParser
.create()
.annotations(BeanAnnotation.create(Foo.class).typeName("foo").build())
.beanDictionary(Foo.class)
.build();
Object o = jp.parse(json, Object.class);
System.err.println(o.getClass().getName()); // Prints "org.apache.juneau.Test$Foo"
}
public static class Foo {
public String bar = "x";
}
}
from juneau.
Thanks again. I confirmed this is working.
from juneau.
FYI...there's a convenience method for this:
WriterSerializer serializer = JsonSerializer
.create()
.typeName(MyBean.class, "mybean")
.build();
from juneau.
Related Issues (1)
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 juneau.