Mapping query string to object with AutoMapper - query-string

I have discovered AutoMapper recently, and I´m thinking if I could mapping HttpConext, (really query parameters) to a typed object.

The HttpRequest.QueryString is a NameValueCollection. So you could create some custom mapping for this like in the example here.

Related

Storing List<Map<Enum,Object>> datatype as JSON in DynamoDb

In Java, when using DynamoDbMapper, what is the best way to store List<Map<Enum,Object>> datatype as JSON? Which annotations should we use to achieve this? In our case, the Enum could be serialized into a simple string, while the Object could be serialized into JSON string.
Also, could anyone clarify in which situation one must use each of these annotations #DynamoDBTypeConverter, #DynamoDBTypeConvertedJson, and #DynamoDbDocument?

Convert Doctrine 2 result object (entity) to array?

I know that it is possible to specify that you want array type instead of object type when you run query with Doctrine. However, I happen to be working with the code that I can't edit which returns to me the result from a query as an object and I want to be able to convert that to array somehow. It seems like in the older version doctrine used to have something like toArray() which can be used.
Is there something similar to that now which I can use?
No, doctrine 2 uses the data mapper pattern and doesn't make any assumptions about the PHP class. If the class doesn't provide a toArray() method explicitly, then you'd need to create the array manually with the object's getter methods.

Json.net deserialization of data type ConcurrentBag

I have an object with a property of type ConcurrentBag<object>.
When I am trying to deserialize a json string to my object using JsonConvert.DeserializeObject(), I get an exception which indicates the serializer is failing to convert from an array to the object's ConcurrentBag<object> data type.
Any help to solve this problem would be appreciated.
I think you will have to write a converter of sorts. Kinda like this post:
json.net: specify converter for dictionary keys
This website also has some more examples:
http://weblogs.asp.net/thangchung/archive/2010/08/26/customizing-the-converter-for-json-net.aspx

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?

is there a way to populate an existing object from json using json.net based upon an interface

Looking for a way to do something like the following:
NewtonSoft.Json.JsonConvert.Populate<IMyContract>(jsonStr, currentObj);
where the json.net engine would only attempt to populate the properties that are identified in the IMyContract.
Any suggestions?
Maybe u can use
PopulateObject(String, Object, JsonSerializerSettings)
And use your own JsonSerializerSettings. See: http://james.newtonking.com/projects/json/help/html/T_Newtonsoft_Json_JsonSerializerSettings.htm
Try to use one of the delegates to restrict the properties that are set yourself. That can be based on an Interface, using reflection.
I don't know any other way. If you don't get it with the JsonSerializerSettings you can always write your own PopulateObject method using the Json framework.

Resources