which Settings plugin to use in MvvmCross - 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.

Related

How to Store a BluetoothDevice Object to Shared Preferences in my Flutter App

I am using flutter_blue package to get the Bluetooth device. After connection i am trying to store the device locally so that the device info can be seen even if the Bluetooth device is inactive.
the problem I am finding is I cant seem to save the BluetoothDevice object along with its device info since shared preferences doesn't support object type Bluetoothdevice and that there isn't any toJson or fromJson methods provided in the package. I have heard some people say to serialize it but I am still confused. Could someone please help me since I don't understand what to do.
The BluetoothDevice object does not look like information you'd usually cache, since the identifiable features for a BLE Device are usually masked due to various privacy settings.
Still, if you want to store it in the SharedPreferences. Try making a map containing the fields from the class BluetoothDevice and using SharedPreferences to store that. I seem to remember that the serialization from a map to a JSON string is done internally by the library.
I personally use Extension methods to extend the functionality on classes defined by third-party libraries. You can use an extension to define the toJson and fromJson classes that you mention.

Newtonsoft.Json as configuration provider in ASP.NET (5+)

For better or worse our codebase relies heavily on Newtonsoft.Json. There are various type converters etc. based on this framework and it is simply not worth the effort to rewrite them using some other JSON framework (if even possible).
We use appsettings.json (+ other custom files) to load settings into our applications.
Typically we used to configure this like:
configurationBuilder.AddJsonFile(pathToFile, ...);
Now, we need to use some of the converters we usually rely on when parsing regular JSON for the settings JSON as well. These are usually being automatically picked up by Newtonsoft.Json so we thought the solution would simply be to reference
Microsoft.Extensions.Configuration.NewtonsoftJson and change to:
configurationBuilder.AddNewtonwoftJsonFile(pathToFile, ...);
However this does not appear to be the case. The converters are not being invoked when we call.
var someSettings = configurationSection.Get<SomeSettings>();
If we paste the same settings into a string and manually parse it the usual Newtonsoft.Json way. This works just fine.
Our conclusion is that either we are doing something wrong (hopefully) or the "binding" part where a configuration section is transferred into actual properties on the object is not part of the Newtonsoft.Json configuration extension.
Any suggestions?

How to write an object to gcp object store with x-goog-if-generation-match from a cloud function

I'd like to write an object to gcp object store, while using the x-goog-if-generation-match feature. Using #google-cloud/storage npm library, the file object does not seem to have an option for setting the required object generation.
What are the alternatives?
As you noticed, the #google-cloud/storage npm library doesn't support generation and metageneration preconditions.
As an alternative, you may use either the Storage XML API or the Storage JSON API which do support it. Depending on if you want to use one or the other, you'll be able to use preconditions via HTTP Headers or query string parameters. You'll find the whole list of those here.
Another alternative is to use some kind of optimistic locking:
get the generation id
write object
get the generation id again
repeat until generation after = generation before + 1

Is it possible to automatically pretty print json on REPL with play?

While working on scala REPL with Play Json support(play.api.libs.json), it possible to specify that all REPL output of JsValues should be automatically formatted?
e.g. By specifying another formatter via implicits
I know Json.prettyPrint does JSON pretty on Play, I was interested to know if there is a mechanism, either in play.json to specify a formatter to be used on toString, or if there is a Scala 2.10 construct I can use to automatically wrap every call with minimal or preferably no overhead to the calls.
Probably in Play 2.1+ with Scala there Json.prettyPrint should work (can't confirm now, have no any Scala project atm, but doc says that).
On the other hand, if only destination for prettifying the JSON is possibility to check it 'with your own eyes' I'd rather suggest to leave it in compressed form, and then use some browser plugin to display it in prettified version, it also validates the code, allows to fold the sections etc. ie this one: https://chrome.google.com/webstore/detail/jsonview/chklaanhfefbnpoihckbnefhakgolnmc
What's most important you still save your transfer as just source JSON is still compressed ;)

Binding JSON to nested Grails Domain Objects

I'm developing a RESTful interface which is used to provide JSON data for a JavaScript application.
On the server side I use Grails 1.3.7 and use GORM Domain Objects for persistence. I implemented a custom JSON Marshaller to support marshalling the nested domain objects
Here are sample domain objects:
class SampleDomain {
static mapping = { nest2 cascade: 'all' }
String someString
SampleDomainNested nest2
}
and
class SampleDomainNested {
String someField
}
The SampleDomain resource is published under the URL /rs/sample/ so /rs/sample/1 points to the SampleDomain object with ID 1
When I render the resource using my custom json marshaller (GET on /rs/sample/1), I get the following data:
{
"someString" : "somevalue1",
"nest2" : {
"someField" : "someothervalue"
}
}
which is exactly what I want.
Now comes the problem: I try to send the same data to the resource /rs/sample/1 via PUT.
To bind the json data to the Domain Object, the controller handling the request calls def domain = SampleDomain.get(id) and domain.properties = data where data is the unmarshalled object.
The binding for the "someString" field is working just fine, but the nested object is not populated using the nested data so I get an error that the property "nest2" is null, which is not allowed.
I already tried implementing a custom PropertyEditorSupport as well as a StructuredPropertyEditor and register the editor for the class.
Strangely, the editor only gets called when I supply non-nested values. So when I send the following to the server via PUT (which doesn't make any sense ;) )
{
"someString" : "somevalue1",
"nest2" : "test"
}
at least the property editor gets called.
I looked at the code of the GrailsDataBinder. I found out that setting properties of an association seems to work by specifying the path of the association instead of providing a map, so the following works as well:
{
"someString" : "somevalue1",
"nest2.somefield" : "someothervalue"
}
but this doesn't help me since I don't want to implement a custom JavaScript to JSON object serializer.
Is it possible to use Grails data binding using nested maps? Or do I really heave to implement that by hand for each domain class?
Thanks a lot,
Martin
Since this question got upvoted several times I would like to share what I did in the end:
Since I had some more requirements to be implemented like security etc. I implemented a service layer which hides the domain objects from the controllers. I introduced a "dynamic DTO layer" which translates Domain Objects to Groovy Maps which can be serialized easily using the standard serializers and which implements the updates manually. All the semi-automatic/meta-programming/command pattern/... based solutions I tried to implement failed at some point, mostly resulting in strange GORM errors or a lot of configuration code (and a lot of frustration). The update and serialization methods for the DTOs are fairly straightforward and could be implemented very quickly. It does not introduce a lot of duplicate code as well since you have to specify how your domain objects are serialized anyway if you don't want to publish your internal domain object structure. Maybe it's not the most elegant solution but it was the only solution which really worked for me. It also allows me to implement batch updates since the update logic is not connected to the http requests any more.
However I must say that I don't think that grails is the appropriate tech stack best suited for this kind of application, since it makes your application very heavy-weight and inflexbile. My experience is that once you start doing things which are not supported by the framework by default, it starts getting messy. Furthermore, I don't like the fact that the "repository" layer in grails essentially only exists as a part of the domain objects which introduced a lot of problems and resulted in several "proxy services" emulating a repository layer. If you start building an application using a json rest interface, I would suggest to either go for a very light-weight technology like node.js or, if you want to/have to stick to a java based stack, use standard spring framework + spring mvc + spring data with a nice and clean dto layer (this is what I've migrated to and it works like a charm). You don't have to write a lot of boilerplate code and you are completely in control of what's actually happening. Furthermore you get strong typing which increases developer productivity as well as maintainability and which legitimates the additional LOCs. And of course strong typing means strong tooling!
I started writing a blog entry describing the architecture I came up with (with a sample project of course), however I don't have a lot of time right now to finish it. When it's done I'm going to link to it here for reference.
Hope this can serve as inspiration for people experiencing similar problems.
Cheers!
It requires you to provide teh class name:
{ class:"SampleDomain", someString: "abc",
nest2: { class: "SampleDomainNested", someField:"def" }
}
I know, it requires different input that the output it produces.
As I mentioned in the comment earlier, you might be better off using the gson library.
Not sure why you wrote your own json marshaller, with xstream around.
See http://x-stream.github.io/json-tutorial.html
We have been very happy with xstream for our back end (grails based) services and this way you can render marshall in xml or json, or override the default marshalling for a specific object if you like.
Jettison seems to produce a more compact less human readable JSON and you can run into some library collision stuff, but the default internal json stream renderer is decent.
If you are going to publish the service to the public, you will want to take the time to return appropriate HTTP protocol responses for errors etc... ($.02)