johnzon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Romain Manni-Bucau <rmannibu...@gmail.com>
Subject Re: Instantiation Patterns...
Date Sat, 24 Jun 2017 13:07:17 GMT
What about adding @JsonbCreator on the constructor and moving the property
to the constructor arguments?


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<https://blog-rmannibucau.rhcloud.com> | Old Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | JavaEE Factory
<https://javaeefactory-rmannibucau.rhcloud.com>

2017-06-24 15:05 GMT+02:00 James Carman <james@carmanconsulting.com>:

> Okay, let me fully explain what I'm trying to do.  I'll take a simple
> example.  I'm trying to read data from Consul.  So, I'm using a
> WebTarget-based implementation to talk to Consul, but I'm trying to use
> JSON-B to parse the response.  I have a ConsulNode class:
>
> public class ConsulNode {
>
> @JsonbProperty("Key")
> private final String key;
>
> @JsonbProperty("Value")
> private final String value;
>
> public ConsulNode(String key, String value) {
> this.key = key;
> this.value = value;
> }
>
> public String getKey() {
> return key;
> }
>
> public String getValue() {
> return value;
> }
> }
>
> When I attempt to parse the response into a List<ConsulNode>, I get the
> following exception:
>
> javax.json.bind.JsonbException: class
> org.microbule.config.consul.ConsulNode not instantiable
>
> at org.apache.johnzon.jsonb.JohnsonJsonb.fromJson(JohnsonJsonb.java:160)
> at
> org.microbule.config.consul.ConsulConfigProviderTest.parseResponse(
> ConsulConfigProviderTest.java:83)
> at
> org.microbule.config.consul.ConsulConfigProviderTest.testGetConfig(
> ConsulConfigProviderTest.java:75)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
> 62)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(
> DelegatingMethodAccessorImpl.java:43)
> at java.lang.reflect.Method.invoke(Method.java:497)
> at
> org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(
> FrameworkMethod.java:50)
> at
> org.junit.internal.runners.model.ReflectiveCallable.run(
> ReflectiveCallable.java:12)
> at
> org.junit.runners.model.FrameworkMethod.invokeExplosively(
> FrameworkMethod.java:47)
> at
> org.junit.internal.runners.statements.InvokeMethod.
> evaluate(InvokeMethod.java:17)
> at
> org.junit.internal.runners.statements.RunBefores.
> evaluate(RunBefores.java:26)
> at
> org.junit.internal.runners.statements.RunAfters.evaluate(
> RunAfters.java:27)
> at
> org.junit.rules.ExpectedException$ExpectedExceptionStatement.
> evaluate(ExpectedException.java:239)
> at org.junit.rules.RunRules.evaluate(RunRules.java:20)
> at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
> at
> org.junit.runners.BlockJUnit4ClassRunner.runChild(
> BlockJUnit4ClassRunner.java:78)
> at
> org.junit.runners.BlockJUnit4ClassRunner.runChild(
> BlockJUnit4ClassRunner.java:57)
> at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
> at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
> at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
> at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
> at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
> at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
> at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
> at
> com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(
> JUnit4IdeaTestRunner.java:68)
> at
> com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.
> startRunnerWithArgs(IdeaTestRunner.java:51)
> at
> com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(
> JUnitStarter.java:242)
> at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
> Caused by: org.apache.johnzon.mapper.MapperException: class
> org.microbule.config.consul.ConsulNode not instantiable
> at
> org.apache.johnzon.mapper.MappingParserImpl.buildObject(
> MappingParserImpl.java:281)
> at
> org.apache.johnzon.mapper.MappingParserImpl.toObject(
> MappingParserImpl.java:484)
> at
> org.apache.johnzon.mapper.MappingParserImpl.mapCollection(
> MappingParserImpl.java:603)
> at
> org.apache.johnzon.mapper.MappingParserImpl.readObject(
> MappingParserImpl.java:175)
> at
> org.apache.johnzon.mapper.MappingParserImpl.readObject(
> MappingParserImpl.java:127)
> at
> org.apache.johnzon.mapper.MappingParserImpl.readObject(
> MappingParserImpl.java:117)
> at org.apache.johnzon.mapper.Mapper.mapObject(Mapper.java:236)
> at org.apache.johnzon.mapper.Mapper.readCollection(Mapper.java:207)
> at org.apache.johnzon.jsonb.JohnsonJsonb.fromJson(JohnsonJsonb.java:151)
> ... 28 more
>
> On Sat, Jun 24, 2017 at 8:55 AM Romain Manni-Bucau <rmannibucau@gmail.com>
> wrote:
>
> > Have to admit I don't fully see the use case since noarg contructors are
> > not mandatory but JOHNZON-130 opens the doort to custom extensions.
> >
> > If the need is something like myInstance == jsonMapper.map(myInstance,
> > input) it shouldnt be hard to extract it from our mapper but not sure how
> > we would wire it in jsonb api, probably accesMode can help with a
> > threadlocal or so.
> >
> >
> > Romain Manni-Bucau
> > @rmannibucau <https://twitter.com/rmannibucau> |  Blog
> > <https://blog-rmannibucau.rhcloud.com> | Old Blog
> > <http://rmannibucau.wordpress.com> | Github <
> > https://github.com/rmannibucau> |
> > LinkedIn <https://www.linkedin.com/in/rmannibucau> | JavaEE Factory
> > <https://javaeefactory-rmannibucau.rhcloud.com>
> >
> > 2017-06-24 14:49 GMT+02:00 James Carman <james@carmanconsulting.com>:
> >
> > > Well, my use case is that I want to let Johnzon instantiate objects
> when
> > > there is no special annotation or factory method present.  I just want
> it
> > > to instantiate the object and then populate the fields.  I realize this
> > is
> > > somewhat niche, so I don't know if I'll push a PR for the
> implementation,
> > > unless you guys want me to.  This will mimic what I currently have with
> > > GSON which proves to be very convenient, especially in JAX-RS.
> > >
> > > On Sat, Jun 24, 2017 at 8:28 AM Romain Manni-Bucau <
> > rmannibucau@gmail.com>
> > > wrote:
> > >
> > > > Internals:
> > > >
> > > > in general you do builder.setAccessMode(accessMode);
> > > > but in jsonb context we didnt wire it yet cause we enforce
> > > > JsonbAccessMode but
> > > > we can surely still expose it (in particular read from jsonbconfig
> > > > properties the delegate).
> > > >
> > > > Spec:
> > > >
> > > > In jsonb we have @JsonbCreator support for constructors.
> > > >
> > > >
> > > > Romain Manni-Bucau
> > > > @rmannibucau <https://twitter.com/rmannibucau> |  Blog
> > > > <https://blog-rmannibucau.rhcloud.com> | Old Blog
> > > > <http://rmannibucau.wordpress.com> | Github <
> > > > https://github.com/rmannibucau> |
> > > > LinkedIn <https://www.linkedin.com/in/rmannibucau> | JavaEE Factory
> > > > <https://javaeefactory-rmannibucau.rhcloud.com>
> > > >
> > > > 2017-06-24 14:19 GMT+02:00 James Carman <james@carmanconsulting.com
> >:
> > > >
> > > > > Are there docs on how to provide my own AccessMode implementation?
> > > > >
> > > > >
> > > > >
> > > > > On Sat, Jun 24, 2017 at 8:16 AM Romain Manni-Bucau <
> > > > rmannibucau@gmail.com>
> > > > > wrote:
> > > > >
> > > > > > Using a custom accessmode you can.
> > > > > >
> > > > > > Side note: we have constructor support I think.
> > > > > >
> > > > > > Le 24 juin 2017 13:59, "James Carman" <
> james@carmanconsulting.com>
> > a
> > > > > > écrit :
> > > > > >
> > > > > > > Current, with Johnzon, I can't instantiate objects for
classes
> > > where
> > > > I
> > > > > > have
> > > > > > > no default constructor available.  With GSON, it supports
this
> > > using
> > > > > the
> > > > > > > Unsafe class I believe.  Is there a way for me to provide
my
> own
> > > > > > > instantiator implementation (I'd probably pick Objenesis)?
 I
> > don't
> > > > > want
> > > > > > to
> > > > > > > have to provide default constructors for all of my immutable
> > > > "request"
> > > > > > > objects when using Johnzon in my JAX-RS MessageBodyReader/
> > > > > > > MessageBodyWriter.
> > > > > > >
> > > > > > > Thanks,
> > > > > > >
> > > > > > > James
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message