I am trying to use flexjson in a playframework java project. But I am unable to deserialize json string to object. I am getting Could not load class exception while trying to deserialize caused by class not fount exception. I am using google guice for dependency injection does it have any thing to do with this exception.
Resolved after upgrading the lib to 3.1
Related
Previously I was using .Net Core 2.2, I was able to send a json object from a service class using hub context and SendAsync method to a front end web client. Im having issues after I've upgraded my project framework to 3.1. If i call the SendAsync method using a json object, it will hit an error stating "{"The collection type 'Newtonsoft.Json.Linq.JObject' is not supported."}", if I send any other class object it will directly go to OnDisconnected.
Sample of the method i use
Using Json Object: await _hubContext.Clients.Group(groupName).SendAsync("NotificationResponse", jsonObject);
Using Class Object: await _hubContext.Clients.Group(groupName).SendAsync("NotificationResponse", notificationObject);
I've tried sending object directly from the hub, I was able to send a normal class object but not a json object. I did the testing on my project and also sample from this tutorial: https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-3.1&tabs=visual-studio
I posted this question on github and got the answer from BrennanConroy. Thanks alot !
His answer was:
2.1 was using Newtonsoft internally for Json, 3.1 uses System.Text.Json. If you're using features that don't work with System.Text.Json you can switch back to Newtonsoft https://learn.microsoft.com/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#switch-to-newtonsoftjson
After doing this it fixed the issue.
We are using spring 4.3. Inside the restController we're obtaining a HashMap for the JSON payload, and then explicitly converting it into specific pojos, since the pojos have to retrieved at runtime. The problem we're facing is Spring 4.1 started using GSON instead of Jackson, now instead of casting to Hashmap GSON is converting our json to LinkedTreeMap. Is there anyway we can force GSON to use HASHMAP instead of LinkedTreeMap or force spring framework to use Jackson for serialization instead of GSON?
Moreover when does GSON uses LinkedTreeMap and when Hashmap?
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to java.util.HashMap
Thanks
I'm in the process of upgrading a grails plugin from 1.3.4 to grails 2.1.1. After upgrading I now have an integration test that fails that was not failing before. It fails on using the "as JSON" (grails.converters.JSON).
#Test
public void testConvertCollectionOfEnvironmentSettingsToJSON() {
EnvironmentSetting setting = configurationService.getEnvironmentSetting('ENFORCE_SCHEMA_INSTANCE_RULE')
def jsonSetting = setting as JSON //exception thrown here
def s = jsonSetting as String
assertNotNull jsonSetting
}
The exception and stacktrace:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.company.ipsvc.configuration.domain.EnvironmentSettingAllRevs#48c12420' with class 'com.company.ipsvc.configuration.domain.EnvironmentSettingAllRevs' to class 'grails.converters.JSON'
at com.company.ipsvc.configuration.converter.json.basic.BasicEnvironmentSettingJSONIntegrationTests.testConvertCollectionOfEnvironmentSettingsToJSON(BasicEnvironmentSettingJSONIntegrationTests.groovy:28)
I am able to use encodeAsJSON() successfully. I also have the same issue with as XML.
I think converters (as JSON syntax) will only work on domain objects and collections by default.
To convert arbitrary objects you should use the encodeAsJSON() converter, I believe. Or use an object marshaller, where you tell the converter how to deal with your object.
The docs aren't very clear on this though..
See:
http://grails.org/Converters+Reference (object marshalling section at bottom)
http://grails.org/doc/latest/ref/Plug-ins/codecs.html
But I note that http://grails.org/doc/latest/api/grails/converters/JSON.html#JSON%28java.lang.Object%29 says that the object converts POGOs.. Maybe it means if you have a marshaller?
I did find this reference too:
Notice that the ‘as’ operator is not overloaded for plain objects ...
Domain objects can use the ‘as’ operator to cast an object to JSON, the same as a collection. So unlike POGOs, where they must be massaged into a list or have encodeAsJSON explictly called ...
http://manbuildswebsite.com/2010/02/08/rendering-json-in-grails-part-2-plain-old-groovy-objects-and-domain-objects/
Which seems to describe the situation.
For non-Domain objects, we found that this would crop up when running tests... the solution for us was to use new JSON:
render new JSON( obj )
This would allow the test to work, and the code does the same thing (essentially)
Ran into a similar issue that broke unit test using grails 2.2.1 . At issue was a straight obj as JSON conversion attempt. But this was interpreted as type casting instead.
The workaround is to stuff your obj to be converted into a map like this [data:obj] as JSON
I am using Gson 1.6 and Spring Framework 3.0 for a Java web app on WebSphere 6.1. I have some Spring beans for which the actual instance is a CGLIB proxy. When I attempt to serialize these beans via Gson, the non-primitive properties of the class are not serialized. Instead I get something like:
{
"CGLIB$BOUND":true,
"CGLIB$CONSTRUCTED":true,
"booleanProperty":true,
"anotherBooleanProperty":true,
}
where I was expecting something more like
{
"stringProperty":"stringValue"
"integerObjectProperty":17,
"booleanProperty":true,
"anotherBooleanProperty":true,
}
When I serialize a non-proxied POJO, the output is exactly as I'd expect. How can I get Gson to generate the output I expect?
I'd say your problem is the result of a bad practice.
Spring Beans are usually defined by behaviour, not state. And you should only serialize Classes that have State, not behaviour.
Refactor your code, transfer the state from the Beans to Value Objects, and serialize those.
I would consider trying out another JSON processor, Jackson (http://jackson.codehaus.org), since it has some support for dealing with cglib proxied objects. And Spring supports Jackson so you have less code to write, compared to gson-based version.
I would like to use Lift-JSON (v2.2) with Play framework to parse a JSON file into Scala case classes. I am using scala 2.8.1 and play 1.1 with scala pluging v0.8. To start I copied the code from https://github.com/lift/lift/tree/master/framework/lift-base/lift-json/ for extracting value into classes (Person class, section "Extracting values". When I browse to localhost to see the results I receive
play.exceptions.JavaExecutionException: Parsed JSON values do not match with class constructor
args=
arg types=
constructor=public controllers.Person(java.lang.String,controllers.Address,scala.collection.immutable.List)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:285)
at Invocation.HTTP Request(Play!)
Caused by: net.liftweb.json.MappingException: Parsed JSON values do not match with class constructor
args=
arg types=
constructor=public controllers.Person(java.lang.String,controllers.Address,scala.collection.immutable.List)
at net.liftweb.json.Meta$.fail(Meta.scala:128)
...
I suppose Play somehow runs scala commands in REPL mode ( the problem discussed here: http://caffiendfrog.blogspot.com/2010/11/scala-json-lift-web-trouble-with.html ). I appreciate your experience with using play and lift-json to parse/extract json objects.
I switched from lift-json to https://github.com/codahale/jerkson , problem resolved.
You should look another discussion about this problem, it seems to be explained :)