Using json mappers to retrieve and save data with firestore? - firebase

I'm trying to use json mappers to handle my Flutter Firebase Firestore serialization and deserialization. Right now I'm using the json_serializable package, which has gotten me past most hurdles.
I'm seeing an issue when inserting / updating though. I have to be able to get my dart object into the json Map form, not the String form to properly insert it. While the library provides a Map<String, dynamic> toJson() => ... function implementation, this function doesn't convert nested objects, which basically makes it worthless.
To work around this I'm converting the json into a String, then back into a Map doing the following:
final json = jsonEncode(reg);
final jsonMap = jsonDecode(json);
This is obviously bad. How can I get my dart object into a json Map form better?
I'm also having to do weird things, like grab the snapshot data, and grab the document id, and then insert the id into the data map before deserializing it, so I have the id in the data model, and then remove it before serializing. There has to be a better way to auto-serialize and deserialize from firebase. What am I missing?
final snapshot = document.data;
snapshot['id'] = documentID;

Related

Convert a Java class to DynamoDb Map<String, AttributeValue>

I have a class which contains some String and int fields. I would like to convert this class to the Map<String, AttributeValue> representation. I know that DynamoDbMapper is doing this internally, but we are not using DynamoDbMapper and I would like to know if there is any existing library that I can use to perform this conversion?
DynamoDB has a mid-level api that you might find helpful. One of its methods is ItemUtils.toAttributeValues(Item). This method allows you to convert from an Item to an attribute value map.
Now, to get an Item, you can construct one manually (but you don’t want to) or you can construct on from a json blob using Item.fromJson(String).
Now all that remains is for you to use your favorite serializer to convert from your java data model to json. The methods I’ve mentioned seamlessly handle the rest of the conversion.
TLDR;
Pojo --> json --> Item --> Map<String, AttributeValue>

Get the results of a CosmosDb query as a Raw string (payload of the http response)

I'm using the .NET API of CosmosDB and I'm getting a hard time trying to figure out how to get the raw result of a CosmosDB query prior to it getting deserialized into a class. Browsing the documentation, all examples I find cast the results to an specific class or to a dynamic. That is:
//This returns a Document, wich actually is a dynamic...
client.ReadDocumentAsync(...)
//This returns an object of type MyClass, wich I supose is casted internally by the API
client.ReadDocumentAsync<MyClass>(...)
What I want to do is to get the original raw JSON payload of the result to inspect it without the overhead of deserializing it to anything else.
Does anybody know if it's possible to get the raw result with the .NET api? If so, how?
In other cases, I need to use the result as an ExpandoObject to treat it dynamically, but I find that the "dynamic" results given by the api are not "expandables" so I'm forced to serialize them and then deserialize again in a recursive form into an ExpandoObject. Furthermore, the result is polluted with _rid, Etag, etc. properties that I don't need on my object. It's quite anoying.
I think it's an unnecesary overhead to serialize and then deserialize again, so maybe the optimus way would be to get the raw JSON result and write a method to deserialize directly to Expando.
Or maybe I'm loosing any point and there's an API to get the results as Expandos. Does anybody know it?
Check out this question that I had earlier:
Converting arbitrary json response to list of "things"
Altough I didn't named it, the API in question was actually DocumentDb, so I think you'll be able to use that code.
Seen some bad advice here, but it is built into the SDK natively.
Document doc = cosmosClient.ReadDocumentAsync(yourDocLink);
string json = doc.ToString();

Firebase REST API get all data from node as JSON Array

Is there any possibility in Firebase to get data from node as JSON array, but not like multiple objects
For example I have:
https://additive-food-e2a9a.firebaseio.com/additive.json
{"-KjrTAOehl0RAvKYlYVp":{"alias":"Curcumin","danger":"2","name":"E100"},"-KjrUIm-itn8h8EjckNQ":{"alias":"Curcumin","danger":"2","name":"E100"},"-KjrXlvuJjMuogCJmGDK":{"alias":"Curcumin","danger":"2","name":"E100"}}
But I need something like:
[{"-KjrTAOehl0RAvKYlYVp":{"alias":"Curcumin","danger":"2","name":"E100"},"-KjrUIm-itn8h8EjckNQ":{"alias":"Curcumin","danger":"2","name":"E100"},"-KjrXlvuJjMuogCJmGDK":{"alias":"Curcumin","danger":"2","name":"E100"}]
Firebase returns only JSON object when you hit GET request to the node. You have to apply JSON parsing logic in Java, Javascript or whatever language you use.
Sample JSON Parsing logic in Java.

How to get all SolrNet query result info in JSON format?

I am not sure if this is a JSON.Net issue or a SolrNet issue, which is why I am tagging it with both.
I am using SolrNet and currently handling all page interactivity with JavaScript from an MVC call, by returning a JsonNetResult encoding of the object returned by solr.Query(). I am now experimenting with Faceting, but am not seeing the Facet info in my results.
I've got an MVC Controller method like the one below. (JsonNetResult is similar to the stock JsonResult, but encodes JSON using JSON.Net, described here.)
public JsonNetResult Index(string keywords)
{
JsonNetResult jsonNetResult = new JsonNetResult();
var documents = solr.Query(new SolrQuery(keywords), new QueryOptions
{
Rows = 10,
Facet = new FacetParameters
{
Queries = new[] {new SolrFacetFieldQuery("system")}
}
});
jsonNetResult.Formatting = Formatting.Indented;
jsonNetResult.Data = documents;
return jsonNetResult;
}
I was expecting to see the faceting information encoded into JSON in the JsonNetResult, but all it contains is the array of hashes of the documents matching my query. Am I missing something in how SolrNet response objects work, or do I really need to parse through the object myself, and create something that JSON.Net can use to encode all of the information related to my query?
FYI, I have tried using a standard JsonResult in MVC, and the results are the same. Also, the reason I am using SolrNet and not just calling Solr directly and asking for JSON is because we do not want to expose the Solr search engine web interface directly to the user.
Since Solr can respond with JSON, if you want to return JSON directly to the view you'd be incurring some unnecessary overhead by having SolrNet deserialize a XML response, then serialize it to JSON. Instead, use SolrNet's components to skip the response parser. A couple of pointers to do this:
SolrQueryExecuter.GetAllParameters()
Low-level SolrNet
I am not sure if this is the best answer, but I have since been experimenting and found that if I change my original line from:
jsonNetResult.Data = documents;
to:
jsonNetResult.Data = new { Documents = documents, FacetFields = documents.FacetFields };
The data is at least serialized by JSON.Net. I guess I still don't understand the format of the object being returned by SolrNet's Query() method, since it seems like those properties (like FacetFields) should be serialized without me having to explicitly name them like I'm now doing above?

Questions on passing and formatting JSON to HttpResponse

I just wanted to clarify a few questions I have. I'm building a JSON string and returning it using context.response. I'm just calling a url to an .ashx handler and trying to return JSON.
Here's how I have approached it so far:
List products = GetCarolProducts();
List images = new List();
foreach(Product p in products)
{
string imageTag = string.Format(#"<img src=""{0}"" alt=""""></li>", Util.ImageUrl(p.Image, false));
images.Add(imageTag);
i++;
}
string jsonString = images.ToJSON();
context.Response.Write(jsonString);
I'm using the example helper method here for the JSON: http://weblogs.asp.net/scottgu/archive/2007/10/01/tip-trick-building-a-tojson-extension-method-using-net-3-5.aspx
So my questions are:
1) I didn't want to pass back the entire product object. In fact I didn't want to pass that back at all. I wanted to pass back a bunch of image tags that I made in my foreach loop. so what I did was just create that string in each iteration of product and added it to a new generic list of type string. I assume this is fine, and that passing a generic string list can be serialized into JSON...that string is an object itself so this should be fine? thing is, it's not going to have any properties so I don't know. I guess I would be leary if this is going to work as if I were to pass an object such as product for example, it at least has properties in it such as ID, Name, etc.
So if this is not going to work, I am wondering how I can just receive back a list of JSON that gives me . I believe though I still need to be able to access that returned JSON with key/value though. But in terms of data, that's all I need in my jQuery, just a bunch of images.
So therefore my question is, must I pass an object that has properties in it so that my returned JSON has some params I can picked up via the jQuery since jQuery creates a nice typed object for you based on those properties? I don't think sending a generic string list would work in my situation then.
so not quite sure how to set this up and pass only the list of images to my ToJSON helper method and ultimately to my jQuery JSON parsing.
2) Not sure if I need to do anything special to tell the context.response that it's JSON other than pass it the final string that the helper method here creates? with XML you'd have to tell the response to format it as XML with context .Response .ContentType = "text/xml". So do I need to specify anything if I'm passing back JSON strings?
For the response content-type, look at Douglas Crockford's JSONRequest proposal.
Content-Type: application/jsonrequest

Resources