[JSONP-24] Sequence of adding a JsonArrayBuilder into a JsonObjectBuilder interferes in the final result Created: 27/Oct/13  Updated: 27/Oct/13

Status: Open
Project: jsonp
Component/s: None
Affects Version/s: 1.0.3
Fix Version/s: None

Type: Bug Priority: Critical
Reporter: Hildeberto Mendonça Assignee: Unassigned
Resolution: Unresolved Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

JDK 1.7, Glassfish 3.1.2.2, Jersey 1.8


Tags: JsonArrayBuilder, JsonObject, JsonObjectBuilder

 Description   

While creating some Json content using the Object Model API, I realised that the sequence of adding a JsonArrayBuilder into a JsonObjectBuilder interferes in the final result. The following code won't include the categories in the resulting Json because the line, indicated with an arrow, adds the JsonArrayBuilder to the JsonObjectBuilder before JsonArrayBuilder is loaded with data :

List<CategoryMenu> categories = categoryMenuBean.findCategoriesByMenu(menu);
JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
JsonArrayBuilder arrayBuilderCategories = Json.createArrayBuilder();

objectBuilder.add("categories", arrayBuilderCategories); // <--

for(CategoryMenu category: categories) {
     arrayBuilderCategories.add(Json.createObjectBuilder()
                 .add("id", category.getId())
                 .add("name", category.getName()));
}

JsonObject model = objectBuilder.build();
StringWriter strWriter = new StringWriter();
try (JsonWriter jsonWriter = Json.createWriter(strWriter)) {
     jsonWriter.writeObject(model);
}

System.out.println(strWriter.toString()); // output = []

To work around with the problem, I had to move that highlighted line to after the iteration:

List<CategoryMenu> categories = categoryMenuBean.findCategoriesByMenu(menu);
JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
JsonArrayBuilder arrayBuilderCategories = Json.createArrayBuilder();

for(CategoryMenu category: categories) {
     arrayBuilderCategories.add(Json.createObjectBuilder()
                 .add("id", category.getId())
                 .add("name", category.getName()));
}

objectBuilder.add("categories", arrayBuilderCategories); // <--

JsonObject model = objectBuilder.build();
StringWriter strWriter = new StringWriter();
try (JsonWriter jsonWriter = Json.createWriter(strWriter)) {
     jsonWriter.writeObject(model);
}
System.out.println(strWriter.toString()); // output = {"categories":[{"id":1,"name":"Plat du Jour"},{"id":2,"name":"Permanent"}]}

Apparently, the build is performed right away, while data is added to those builder objects. In fact, it should be built only when the method build() is finally invoked. In both examples, it always happen when all data is added to the builders and it is time to build the final JsonObject model. Therefore, both examples above should work normally.






[JSONP-1] JSON not getting deserialized in a WebSocket endpoint Created: 24/Oct/12  Updated: 24/Oct/12

Status: Open
Project: jsonp
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major
Reporter: arungupta Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

@Override
public MyMessage decode(String string) throws DecodeException

{ System.out.println("decoding: " + string); this.jsonObject = new JsonReader(new StringReader(string)).readObject(); System.out.println(jsonObject); return this; }

is throwing the following exception:

INFO: decoding: { }
SEVERE: javax.json.JsonException: Provider org.glassfish.json.JsonProviderImpl not found
at javax.json.spi.JsonProvider.provider(JsonProvider.java:101)
at javax.json.Json.createParser(Json.java:84)
at org.glassfish.jsonapi.JsonReaderImpl.<init>(JsonReaderImpl.java:58)
at javax.json.JsonReader.<init>(JsonReader.java:77)
at org.sample.MyMessage.decode(MyMessage.java:60)
at org.sample.MyMessage.decode(MyMessage.java:53)
at org.glassfish.tyrus.platform.WebSocketEndpointImpl.decodeMessage(WebSocketEndpointImpl.java:212)
at org.glassfish.tyrus.platform.WebSocketEndpointImpl.processMessage(WebSocketEndpointImpl.java:315)
at org.glassfish.tyrus.platform.WebSocketEndpointImpl.onMessage(WebSocketEndpointImpl.java:300)
at org.glassfish.tyrus.spi.grizzlyprovider.GrizzlyEndpoint.onMessage(GrizzlyEndpoint.java:82)
at org.glassfish.grizzly.websockets.DefaultWebSocket.onMessage(DefaultWebSocket.java:164)
at org.glassfish.grizzly.websockets.frametypes.TextFrameType.respond(TextFrameType.java:70)
at org.glassfish.grizzly.websockets.DataFrame.respond(DataFrame.java:104)
at org.glassfish.grizzly.websockets.WebSocketFilter.handleRead(WebSocketFilter.java:221)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:265)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:134)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:825)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:578)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:558)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.ClassNotFoundException: org.glassfish.json.JsonProviderImpl not found by org.glassfish.main.web.core [246]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1460)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:72)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1843)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at javax.json.spi.JsonProvider.provider(JsonProvider.java:96)
... 27 more

SEVERE: at javax.json.spi.JsonProvider.provider(JsonProvider.java:101)
SEVERE: at javax.json.Json.createParser(Json.java:84)
SEVERE: at org.glassfish.jsonapi.JsonReaderImpl.<init>(JsonReaderImpl.java:58)
SEVERE: at javax.json.JsonReader.<init>(JsonReader.java:77)
SEVERE: at org.sample.MyMessage.decode(MyMessage.java:60)
SEVERE: at org.sample.MyMessage.decode(MyMessage.java:53)
SEVERE: at org.glassfish.tyrus.platform.WebSocketEndpointImpl.decodeMessage(WebSocketEndpointImpl.java:212)
SEVERE: at org.glassfish.tyrus.platform.WebSocketEndpointImpl.processMessage(WebSocketEndpointImpl.java:315)
SEVERE: at org.glassfish.tyrus.platform.WebSocketEndpointImpl.onMessage(WebSocketEndpointImpl.java:300)
SEVERE: at org.glassfish.tyrus.spi.grizzlyprovider.GrizzlyEndpoint.onMessage(GrizzlyEndpoint.java:82)
SEVERE: at org.glassfish.grizzly.websockets.DefaultWebSocket.onMessage(DefaultWebSocket.java:164)
SEVERE: at org.glassfish.grizzly.websockets.frametypes.TextFrameType.respond(TextFrameType.java:70)
SEVERE: at org.glassfish.grizzly.websockets.DataFrame.respond(DataFrame.java:104)
SEVERE: at org.glassfish.grizzly.websockets.WebSocketFilter.handleRead(WebSocketFilter.java:221)
SEVERE: at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
SEVERE: at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:265)
SEVERE: at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
SEVERE: at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:134)
SEVERE: at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
SEVERE: at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
SEVERE: at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:825)
SEVERE: at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
SEVERE: at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
SEVERE: at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
SEVERE: at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
SEVERE: at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:578)
SEVERE: at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:558)
SEVERE: at java.lang.Thread.run(Thread.java:722)
SEVERE: Caused by: java.lang.ClassNotFoundException: org.glassfish.json.JsonProviderImpl not found by org.glassfish.main.web.core [246]
SEVERE: at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1460)
SEVERE: at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:72)
SEVERE: at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1843)
SEVERE: at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
SEVERE: at javax.json.spi.JsonProvider.provider(JsonProvider.java:96)
SEVERE: ... 27 more






[JSONP-5] Better JsonGenerator and JsonObjectBuilder Created: 18/Apr/13  Updated: 23/Apr/13

Status: Open
Project: jsonp
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major
Reporter: anthavio Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Recently I've did some builder pattern related coding and then I've come across javax.json api and I think that it can be improved

1. JsonGenerator allows user wrong method calls that throws exceptions.

JsonGenerator jg = javax.json.Json.createGenerator(System.out);
jg.writeStartObject().write("whatever").writeEnd(); //javax.json.stream.JsonGenerationException: write(String) can only be called in array context

2. JsonObjectBuilder methods for nested objects and arrays is somewhat cumbersome because of JsonBuilderFactory usage

Java 5 generics allows to create much neater builders. I've wrote article about this topic today http://anthavio.blogspot.co.uk/2013/04/building-fluent-builder.html
Please consider reusing ideas from my JsonBuilder into javax.json API making it more pleasant to work with



 Comments   
Comment by jitu [ 23/Apr/13 ]

In EA versions, we used builders with generics. JsonGenerator was also based on the builders. Based on other issues like the following, EG settled on the current approach.

https://java.net/jira/browse/JSON_PROCESSING_SPEC-36
https://java.net/jira/browse/JSON_PROCESSING_SPEC-11





[JSONP-9] ObjectBuilder from JsonObject Created: 26/Apr/13  Updated: 26/Apr/13

Status: Open
Project: jsonp
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major
Reporter: eric.jain Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

JsonObject is immutable, so in order to update one or more values, a new JsonObject has to be created. To avoid having to copy each unchanged value explicitly, the JsonObjectBuilder could be initialized with a JsonObject:

JsonObject oldValue = Json.createObjectBuilder()
    .add("firstName", "John")
    .add("lastName", "Smith")
    .add("age", 25).build();
JsonObject newValue = factory.createObjectBuilder(oldValue)
    .add("age", 26).build();

As an optimization, the new JsonObject could be backed by the old JsonObject.






[JSONP-10] JsonValue.MISSING Created: 26/Apr/13  Updated: 26/Apr/13

Status: Open
Project: jsonp
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major
Reporter: eric.jain Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Given the following JSON:

{ 
    "settings" : {
        "dashboard" : { 
            "width" : 42
        }
    }
}

This code extracts the value of the width property:

int width = 0;
JsonObject settings = value.getJsonObject("settings");
if (settings != null) {
    JsonObject dashboard = settings.getJsonObject("dashboard");
    if (dashboard != null) {
        width = dashboard.getInt("width", 0)
    }
}

It would be nice if this could be accomplished in a more concise manner, e.g.

int width = value.findJsonObject("settings").findJsonObject("dashboard").getInt("width", 0)

The findJsonObject would behave like getJsonObject, but return an empty JsonObject instead of null for missing properties.






[JSONP-27] Using OSGi cannot load the service Created: 19/Sep/14  Updated: 19/Sep/14

Status: Open
Project: jsonp
Component/s: None
Affects Version/s: 1.0.4
Fix Version/s: None

Type: Bug Priority: Major
Reporter: bakos.gabor Assignee: kchung
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

OSGi (4.x)



 Description   

I was trying to use the jsonp project as a default implementation for the json-api in an OSGi environment (eclipse 3.7), although the ServiceLoader is not quite OSGi-friendly solution, so no services found for JsonProvider#provider() and because the JsonProvider from the javax.json-api (1.0) was also loaded if I use that JsonProvider from that bundle (that is loaded first, so most probably this happens) it cannot find the default implementation either.
As a workaround we had to use a different (from the javax.json-api bundle, the one of org.glassfish.javax.json) ContextClassLoader for the thread calling the JsonProvider#provider() method and add a service reference to the jsonp bundle (so it can find that service with the provided classloader).
Ideally the would be to make loading the services load in OSGi environments (that would be the goal of the spi-fly project, but unfortunately that is not an option for us yet, it requires OSGi R5 or above, be we have to support eclipse 3.7 for a while), though for us, currently the exposed service would be sufficient with the workaround.
Could you add a service reference to the default implementation or give other satisfying solution that make this plugin work in OSGi R4 environments?






[JSONP-28] Dead link(s) on project page Created: 11/Mar/15  Updated: 11/Mar/15

Status: Open
Project: jsonp
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major
Reporter: keilw Assignee: kchung
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Tags: links

 Description   

At least the "Documentation" link on https://jsonp.java.net/ is dead/no longer working. Should be fixed, at least pointing to an empty or new page.






[JSONP-14] Conversion of Java primitives to JSON Objects Created: 30/May/13  Updated: 30/May/13

Status: Open
Project: jsonp
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Minor
Reporter: daveagp Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Currently, there is no API method that I can see to convert a String, int, etc. into a JSON primitive. It requires a line of code like

static JsonNumber jsonInt(long l)

{ return Json.createArrayBuilder().add(l).build().getJsonNumber(0); }

static JsonNumber jsonReal(double d)

{ return Json.createArrayBuilder().add(d).build().getJsonNumber(0); }

static JsonString jsonString(String S)

{ return Json.createArrayBuilder().add(S).build().getJsonString(0); }

but this is obviously wasteful in its use of an array.

I can try to implement changes to the API and reference implementation to add this functionality without the array, if it would be desired.

Because all Json value objects are interfaces, it seems one would have to add static methods somewhere rather than constructors to anything. It might make the most sense as one of the following

static JsonNumber Json.jsonNumber(long l)
or
static JsonNumber Json.createJsonNumber(long l)
or
static JsonNumber JsonNumber.create(long l)

or Json.mirror(), etc... Not sure if this already fits in some overarching plan. But if you want me to try to contribute just let me know what you think!






Generated at Thu Jun 04 00:04:23 UTC 2015 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.