Dart - HTTP - Encode JSON Object with Array & int properties - http

I'm trying to make a POST request using dart's http package with json data as the body, I'm using a variable of type dynamic to create the json object and so far it works perfectly as long as all the values in the object are strings.
If I assign the value of a property to be an int or List<int> (expecting it to be converted to an array, as expected by the server) dart crashes due to expecting either a Map<String, String> or a List<int> as the type for the body (the exact type it expects is dynamic, but it tries to cast it to Map<String, String> or List<int>).
My question is, is there any workaround to making a http POST request in dart using a object with dynamic property values?

I was able to resolve this issue by using the HttpClient & HttpClientRequest classes from the dart:io package.
I stored the body as a Map<String, dynamic> and json encoded it before writing it to the request stream.

Related

Elasticsearch NEST client datetime deserialization

I'm trying to deserialize my NEST document to datetime, but everything ends up being a string because I'm using System.Object:
var response = this.client.Search<object>(search);
The reason for this is because I'm searching multiple indices and I have to use object type. Since my whole application is returning DateFormatHandling.MicrosoftDateFormat.
I'd like NEST to deserialize datetime to DateTime type, so that my serializer can correctly serialize to MicrosoftDateFormat on API.

Using json mappers to retrieve and save data with firestore?

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;

whats the difference between responseBody and Serialization

In Spring Framework,i am using responseBody annotation and serialization,i learned that responseBody is for HttpMessageConverts,it will return the output to view Resolver and serialization will convert the data in to byte stream and transfer it using version Id and header,here i have a question that, whats the difference between this two ?
Serialization is a computer science concept that describes how a data structure can be broken down and stored. Deserialization is the reverse, taking a stored format and converting it back into a data structure.
#ResponseBody is an annotation that Spring MVC uses on #RequestMapping methods. It tells the DispatcherServlet to take the return value of your handler method and, using an HttpMessageConverter, serialize it and write it directly to the HTTP response OutputStream.
See the javadoc of HttpMessageConverter for a list of implementation classes. You can write byte[], String, InputStream, Resource objects directly to the stream. There also exist HttpMessageConverter classes for converting any object returned by the handler method to JSON or XML.

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