How can I serialize Exceptions using Json.NET on WinRT? - windows-runtime

This works fine on classic .NET:
string json = JsonConvert.SerializeObject(new Exception("Test"));
But it fails on WinRT (RP) with:
Error getting value from 'TargetSite' on 'System.Exception'.
The API 'System.Exception.get_TargetSite()' cannot be used on the current platform. See http://go.microsoft.com/fwlink/?LinkId=248273 for more information.
EDIT:
I also need to transfer the Exception over the wire and deserialize it on the back end side.
Anyone has any workarounds?

Related

SignalR .Net Core 3.1 unable to send object in SendAsync method from service class

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.

Json parsing in IBM Worklight

I am trying to achieve something like which is mentioned in this link
but i don't know whereto write the parsing code. I tried to write it in new method and added the method in "my adapter.xml" but nothing happens. I even don't know how to log in IBM WorkLight. I used WL.logger(some) but its throwing error that "Logger can not be called on an object".
Any help would be appreciated. Thanks in advance.!
In most cases you don't need to manually parse responses because WL adapter framework will do this for you. Anything you retrieve via WL.Server.invokeHttp API will be parsed to JSON automatically unless you specify returnedContentType:"plain". In case you DO need to manually parse JSON you can use JSON.parse() and JSON.stringify() APIs.
Server side logging is achieved via WL.Logger.debug/error/info etc. You can get more info about it here
You don't have to parse JSON data, there are libraries in Javascript to do that. Try JSON.parse(). You should learning how to write adapters and invoking them from clients. The Getting Started modules are a good place to start, specifically Module 4 in your case.

troubleshooting JAXBUnmarshalException: Unexpected end of stream (RESTeasy)

I would appreciate if someone could help me formulate a strategy for fixing the following error, which occurs in no discernible pattern; most of the time there is no error, and I am successful about 90% of the time:
ERROR [org.jboss.resteasy.core.SynchronousDispatcher] Failed executing POST /toys/customer25/insert
org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException: Unexpected end of stream
How would I log this error simply to the console? I'd like to capture the JSON that this method is receiving and figure out what is causing this error. I would be able to see it in the server logs.
What I am doing is POSTing an array of Toy objects to the server, in JSON format. That is, on the client I transform my Toy object into a Toy JSON. The string that I am sending is like this: [{"toyName":"buzzlightyear", "toyMaker":"mattel"}]
My understanding is that when I POST JSON to a jax-rs restful service, a jax-rs implementation (I'm using RESTeasy 1.2) will somehow automagically convert that JSON string into a java Toy object.
My code on the Java JAX-RS restful service for handling a POST which inserts a Toy object is this:
#POST
#Consumes({MediaType.APPLICATION_JSON})
#Produces({MediaType.TEXT_PLAIN})
#Path("toy/{customer}/insert")
public String insertToys(#PathParam("customer") String customer, List<Toy> toys);
How do I log/capture what is wrong with the JSON string? It seems to me that the system is handling the conversion of the JSON to Toy object right when the insertToys() method is called. So it's hard form to figure out how to debug.
Can someone formulate a strategy for me? What is the likelihood that I am passing in malformed JSON, maybe from some unescaped character? Vs mapping entities wrong? Thanks!
Your JSON is incorrect.
The method accepts List, so you must send an array of Toy Objects:
[{"toyName":"buzzlightyear", "toyMaker":"mattel"}]
(note the braces).
For more objects:
[{"toyName":"buzzlightyear", "toyMaker":"mattel"},{"toyName":"rex", "toyMaker":"mattel"}]

How to have Jackson deserialize a cyclical graph that has been run through JSOG.stringify(myCyclicalGraph)

I am currently using the this Jackson plugin
Which serialized my cyclical graphs. Then on the client I use the JSOG to decode the {#ref} objects like this:
JSOG.decode(data)
The problem comes when I am trying to send the json back up to the server. If I don't do anything to the data I get a "Maximum call stack size exceeded", obviously because my js object is cyclical. I try using:
JSOG.stringify(data);
But then Jackson chokes on all the #id and #refs:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "#id"
Has any one figured out how to do this?
Both sides need to be able to support JSOG convention. It is not part of JSON specification, nor can it be automatically detected; so Jackson can not process it without help.

Unable to use "as JSON" after upgrading to grails 2.1.1 from grails 1.3.4

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