Alternatives to Newtonsoft.JSON for Monodroid - json

We currently use Newtonsoft.JSON for notation and serialization/de-serialization of JSON objects in our C# based projects (along the lines of the following), which we also need to share with an application which has been written using Xamarin MonoDroid
[JsonProperty("type")]
public string theType { get; set; }
....
JsonConvert.DeserializeObject<List<OurModel>>(jsonString);
However, the Newtonsoft.JSON isn't compatible with Mono
Presumably, there is an easy solution to handling JSON data BOTH within the C# environment as well as Monodroid? Ideally, so that we can have a common class that is shared between the two projects?

As listed on the Json.NET homepage, "Json.NET supports Windows, Windows Store, Windows Phone, Mono, and Xamarin."
Newtonsoft is the company that makes Json.NET - so the namespace that the library uses is "Newtonsoft.Json". They are both referring to the same library.
I have successfully used Json.NET in my Xamarin.Android applications.
If you aren't sure where to get a copy of the library that will work with your Xamarin.Android application, try using NuGet

Related

System.Text.Json problem that make swagger with .net core 3.1

I am upgrading my 2.2 .net core web app to 3.1 build succeed but
swagger doesn't load and send message Failed to load API definition. and when I inspect the web request I found this error
Schema cannot be generated for type System.Collections.Generic.Dictionary`2[foonamespace.fooclass,System.Collections.Generic.Dictionary`2[System.String,System.String]] as it's not supported by the System.Text.Json serializer2at Swashbuckle.AspNetCore.SwaggerGen.JsonSerializerDataContractResolver.
I have traced then I found the problem in this line of code
public Dictionary<Foo, Dictionary<string, string>> AdditionalData { get; set; }
Did anyone know how to fix it
Try this: https://github.com/dotnet/runtime/issues/30524#issuecomment-524619972, but the author says specifically
'However it still cannot serialize a nested Dictionary such as
Dictionary<int, Dictionary<int, int>> because System.Text.Json doesn't
accept the inner type Dictionary<int, int>'
So in your code I think there is not possible. On .Net 5.0 will be available with system.text.json, but until then, the only solution is to use NewtonsoftJson.

How can I define a Java interface in JRuby rather than implement one?

I would like to invoke a Java API from JRuby that requires a Java interface, not a concrete class. The Java API uses java.lang.reflect.proxy to implement functionality based on the interface. I have found numerous examples of implementing a java interface with JRuby. However, I need to simply define a java interface in JRuby, not implement one. I can of course define an interface in Java and use this as the interface argument to the API via intf.java_class. However, for convenience, I'd like to be able to directly define the interface from JRuby code.
Update:
Here's the Java API I want to invoke from JRuby:
public interface Query {
<T> Collection<T> execute(Class<T> intf, String sql, Object... args);
}
The Java API requires that the intf argument be an interface, not a class. This is due to the fact that the implementation uses java.lang.reflect.proxy to provide a collection of data objects that can be accessed via the provided interface. Java's java.lang.reflect.proxy will only work with interfaces. As an example, suppose I were reading rows from a database that represented log statements. If I were invoking this from java I might define my data via
public interface LogRecord {
int id();
Timestamp when();
String msg();
String level();
String logger();
String thread();
}
and pass LogRecord.class as the first argument to the Query.execute method.
I don't think it can be done, because the Java class would have to inherit from the ruby interface, and apparently Java classes can't inherit from a JRuby class:
https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby#java-classes-cant-inherit-from-a-jruby-class

which Settings plugin to use in MvvmCross

The Settings plugin on NuGet
https://www.nuget.org/packages/MvvmCross.Plugins.Settings/0.0.0.2
seems to be only available for Windows Phone.
The name (MvvmCross.Plugins.Settings) makes it look like an 'official' plugin but it's not.
Confusingly, there is also another one for Windows Phone too:
https://www.nuget.org/packages/MvvmCross.Plugins.Settings.WindowsPhone/0.0.0.2
both have as author ShawnMclean.
There is also this one, but it's only saving basic types (int, string, double, etc) it is not able to save custom objects:
https://github.com/ceton/Mvx.Plugins.Settings
A very simple implementation would be to serialize a value to JSON. The value can be anything.
What are you using to save settings in your MvvmCross apps?
Cheesebaron has a MvvmCross Settings plugin for Droid, Touch and Windows Phone, based on ceton's plugin, with some fixes:
https://github.com/Cheesebaron/Cheesebaron.MvxPlugins
This plugin also only saves basic types. If you want to save serializable types it shouuld be possible to clone the repo and extend the plugin implementations per platform.
Of course, you can always let your application handle the JSON serialization, and store it as strings, maybe make a nice service for this you can use across your application. After all, JSON serialization should be pretty cross-platform.
Depending on complexity of your settings, you can consider using http://www.nuget.org/packages/MvvmCross.HotTuna.Plugin.File/ and just serialize your settings object into XML or JSON.

Basic Query on JAXRS and tomee

My understanding regarding TomEE (or any Java EE 6 container) is all that i just need to deploy a JAX-RS based application with correct service class annotated.
#Path("/hr")
public class HelloRest {
#GET
#Produces(MediaType.APPLICATION_JSON)
public String getClichedMessage() {
return "Hello World";
}
}
The TomEE container SHOULD take care of converting the "hello world" to proper json format {"hello world"}
I was wondering why TomEE is not converting it to JSON format. Please note I've not used any spring configuration for CXF as I expect this to work with/without CXF configuration.
The certified Web Profile implementation of TomEE does not implement JAX-RS, since JAX-RS is not part of the Web Profile in Java EE 6 (it is in Java EE 7 though).
However, since it appeared that there actually IS very useful stuff in Java EE 6 that goes beyond the Web Profile, TomEE has 2 extra distributions that contains some of this extra stuff.
On the download page you'll find the "JAX-RS" and "Plus" distributions that both contain JAX-RS. With those (or any full profile Java EE AS) you indeed don't need any configuration.
Produces/Consumes doesnt mean convert, it is mainly http headers. String in json is a...string. depend of course of your body writer.

How to deserialize JSON in a Windows 8 App seamlessly

The Web API Client Library NuGet Packag brings in JSON.NET and some handy extension methods for deserializing JSON like so:
response.Content.ReadAsAsync<IEnumerable<Product>>().Result
But this package is not compatible with Windows 8 App projects. How can I deserialize my JSON objects in the same generic way without this NuGet package?
What do you mean by saying "not compatible with Win 8"?
The Microsoft.AspNet.WebApi.Client has been developed to support Win8.
Install-Package Microsoft.AspNet.WebApi.Client
Install just this package, not entire Web API.
You can see the release notes here, in the blog post by Henrik Frystyk Nielsen - http://blogs.msdn.com/b/henrikn/archive/2012/08/15/asp-net-web-api-released-and-a-preview-of-what-s-next.aspx
The Microsoft ASP.NET Web API Client Libraries package adds support for formatting
content negotiation to System.Net.Http when writing Windows Store Apps.
It includes support for JSON, XML, form URL encoded data, as well as MIME multipart.
This package requires Visual Studio 2012 and Windows 8.
There are at least 2 JSON serializers available for Windows Store apps:
DataContractJsonSerializer is a part of .NET framework.
Json.NET is supported as well. You can add it into a Windows Store app project if you install just this package: Install-Package Newtonsoft.Json
What exactly are you refering to with "deserialize my JSON objects in the same generic way"?