convert javascript object into c# object - c#

I am working on client side programming and I want to convert a javascript object into c# object so that I can save it in database.
Is there any method to achieve this ?
Thanks

javascript:
var obj = {id:0,userName:'Bill'};
c#,
define class:
public class myClass
{
public int id;
public string userName;
}
then transport obj to server (for example using AJAX request), and deserialize, when you need work with object
myClass obj = (new JavascriptSerializer()).Deserialize<myClass>(objJson);
But you can do without deserialization and store objJson string to database

First you will need to pass the JSON object to the server side possibly using a web service in code behind and then you could try this awesome library:
JSON.net
The biggest hurdle you're going to have to overcome here regardless of what serialization/deserialzation technique you decide to go with is how you're going to pass the JSON string from the client side to your code behind where you can use it. For this I recommend using jQuery's AJAX capabilities. This article proved invaluable to me when I was learning how to pass information from the client

For this solution to work you need the type represented by T to be of a type that is decorated with DataContract and DataMember attributes. The DataContractJsonSErializer is WAY better than JavascriptSerializer. This is from some code I wrote for a C# REST client. It does show how to serialize and deserialize generically though. Example object included
[DataContract]
public SampleObject
{
[DataMember("MyProperty")]
public string MyProperty {get;set;}
}
private static byte[] SerializeRequest<T>(string contentType, T request)
{
using (MemoryStream requestObjectStream = new MemoryStream())
{
if (contentType == "applicaton/json")
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
serializer.WriteObject(requestObjectStream, request);
}
else if (contentType == "application/xml")
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(requestObjectStream, request);
}
else
{
throw new Exception("invalid contentType");
}
requestObjectStream.Position = 0;
return requestObjectStream.ToArray();
}
}
private static T DeserializeResponse<T>(string acceptHeader, string responseString)
{
if (acceptHeader == "applicaton/json")
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
MemoryStream responseObjectStream = new MemoryStream(Encoding.UTF8.GetBytes(responseString));
return (T)serializer.ReadObject(responseObjectStream);
}
else if (acceptHeader == "application/xml")
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
MemoryStream responseObjectStream = new MemoryStream(Encoding.UTF8.GetBytes(responseString));
return (T)serializer.ReadObject(responseObjectStream);
}
return default(T);
}

Related

How to use Json.NET for JSON modelbinding in an MVC5 project?

I've been looking around the internet for an answer or example, but could not find one yet. I simply would like to change the default JSON serializer which is used to deserialize JSON while modelbinding to JSON.NET library.
I've found this SO post, but cannot implement it so far, I can't even see the System.Net.Http.Formatters namespace, nor can I see GlobalConfiguration.
What am I missing?
UPDATE
I have an ASP.NET MVC project, it was basically an MVC3 project. Currently I'm targetting .NET 4.5 and using the ASP.NET MVC 5 and related NuGet packages.
I don't see the System.Web.Http assembly, nor any similar namespace. In this context I would like to inject JSON.NET to be used as the default model binder for JSON type of requests.
I've finally found an answer. Basically I don't need the MediaTypeFormatter stuff, that's not designed to be used in MVC environment, but in ASP.NET Web APIs, that's why I do not see those references and namespaces (by the way, those are included in the Microsoft.AspNet.WeApi NuGet package).
The solution is to use a custom value provider factory. Here is the code required.
public class JsonNetValueProviderFactory : ValueProviderFactory
{
public override IValueProvider GetValueProvider(ControllerContext controllerContext)
{
// first make sure we have a valid context
if (controllerContext == null)
throw new ArgumentNullException("controllerContext");
// now make sure we are dealing with a json request
if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
return null;
// get a generic stream reader (get reader for the http stream)
var streamReader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
// convert stream reader to a JSON Text Reader
var JSONReader = new JsonTextReader(streamReader);
// tell JSON to read
if (!JSONReader.Read())
return null;
// make a new Json serializer
var JSONSerializer = new JsonSerializer();
// add the dyamic object converter to our serializer
JSONSerializer.Converters.Add(new ExpandoObjectConverter());
// use JSON.NET to deserialize object to a dynamic (expando) object
Object JSONObject;
// if we start with a "[", treat this as an array
if (JSONReader.TokenType == JsonToken.StartArray)
JSONObject = JSONSerializer.Deserialize<List<ExpandoObject>>(JSONReader);
else
JSONObject = JSONSerializer.Deserialize<ExpandoObject>(JSONReader);
// create a backing store to hold all properties for this deserialization
var backingStore = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
// add all properties to this backing store
AddToBackingStore(backingStore, String.Empty, JSONObject);
// return the object in a dictionary value provider so the MVC understands it
return new DictionaryValueProvider<object>(backingStore, CultureInfo.CurrentCulture);
}
private static void AddToBackingStore(Dictionary<string, object> backingStore, string prefix, object value)
{
var d = value as IDictionary<string, object>;
if (d != null)
{
foreach (var entry in d)
{
AddToBackingStore(backingStore, MakePropertyKey(prefix, entry.Key), entry.Value);
}
return;
}
var l = value as IList;
if (l != null)
{
for (var i = 0; i < l.Count; i++)
{
AddToBackingStore(backingStore, MakeArrayKey(prefix, i), l[i]);
}
return;
}
// primitive
backingStore[prefix] = value;
}
private static string MakeArrayKey(string prefix, int index)
{
return prefix + "[" + index.ToString(CultureInfo.InvariantCulture) + "]";
}
private static string MakePropertyKey(string prefix, string propertyName)
{
return (String.IsNullOrEmpty(prefix)) ? propertyName : prefix + "." + propertyName;
}
}
And you can use it like this in your Application_Start method:
// remove default implementation
ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
// add our custom one
ValueProviderFactories.Factories.Add(new JsonNetValueProviderFactory());
Here is the post which pointed me to the right direction, and also this one gave a good explanation on value providers and modelbinders.
I had such a problem with this as well. I was posting JSON to an action, yet my JsonProperty names were ignored. Thus, my model properties were always empty.
public class MyModel
{
[JsonProperty(PropertyName = "prop1")]
public int Property1 { get; set; }
[JsonProperty(PropertyName = "prop2")]
public int Property2 { get; set; }
[JsonProperty(PropertyName = "prop3")]
public int Property3 { get; set; }
public int Foo { get; set; }
}
I am posting to an action using this custom jquery function:
(function ($) {
$.postJSON = function (url, data, dataType) {
var o = {
url: url,
type: 'POST',
contentType: 'application/json; charset=utf-8'
};
if (data !== undefined)
o.data = JSON.stringify(data);
if (dataType !== undefined)
o.dataType = dataType;
return $.ajax(o);
};
}(jQuery));
And I call it like this:
data = {
prop1: 1,
prop2: 2,
prop3: 3,
foo: 3,
};
$.postJSON('/Controller/MyAction', data, 'json')
.success(function (response) {
...do whatever with the JSON I got back
});
Unfortunately, only foo was ever getting bound (odd, since the case is not the same, but I guess the default modelbinder isn't case-sensitive)
[HttpPost]
public JsonNetResult MyAction(MyModel model)
{
...
}
The solution ended up being rather simple
I just implemented a generic version of Dejan's model binder which works very nicely for me. It could probably use some dummy checks (like making sure the request is actually application/json), but it's doing the trick right now.
internal class JsonNetModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
controllerContext.HttpContext.Request.InputStream.Position = 0;
var stream = controllerContext.RequestContext.HttpContext.Request.InputStream;
var readStream = new StreamReader(stream, Encoding.UTF8);
var json = readStream.ReadToEnd();
return JsonConvert.DeserializeObject(json, bindingContext.ModelType);
}
}
When I want to use it on a specific action, I simply tell it that I want to use my custom Json.Net model binder instead:
[HttpPost]
public JsonNetResult MyAction([ModelBinder(typeof(JsonNetModelBinder))] MyModel model)
{
...
}
Now my [JsonProperty(PropertyName = "")] attributes are no longer ignored on MyModel and everything is bound correctly!
In my case, I had to deserialize complex objects including interfaces and dynamically loaded types etc. so providing a custom value provider does not work as MVC still needs tries to figure out how to instantiate interfaces and then fails.
As my objects were already properly annotated to work with Json.NET, I took a different route: I've implemented a custom model binder and used Json.NET to explicitly deserialize the request body data like this:
internal class CustomModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
// use Json.NET to deserialize the incoming Position
controllerContext.HttpContext.Request.InputStream.Position = 0; // see: http://stackoverflow.com/a/3468653/331281
Stream stream = controllerContext.RequestContext.HttpContext.Request.InputStream;
var readStream = new StreamReader(stream, Encoding.UTF8);
string json = readStream.ReadToEnd();
return JsonConvert.DeserializeObject<MyClass>(json, ...);
}
}
The custom model binder is registered in Global.asax.cs:
ModelBinders.Binders.Add(typeof(MyClass), new CustomModelBinder();

Can I serialize Xml for an MVC Web API without an object type?

So I'm writing my first mvc page, and I'm trying to write a series of routes to allow a reporting system to create simple reports. The xml is small, here is an example:
<xml><root><item><value>23</value></item></root>
I tried this:
using (StringWriter xmlStringWriter = new StringWriter())
{
using (XmlWriter xmlWriter = XmlWriter.Create(xmlStringWriter))
{
XmlWriter.WriteStartElement("root")
...
}
return xmlStringWriter.ToString();
}
but this obviously returns a string and is not interpreted as xml by the browser. I also know* that if you return an object that is serializable then the browser knows to interpret that as xml or json. So I tried defining a set of objects to hold each other in the way the xml is nested:
[Serializable]
public class XmlReportRoot
{
[System.Xml.Serialization.XmlAttribute("root")]
public List<XmlReportItem> item { get; set; }
}
[Serializable]
public class XmlReportItem
{
[System.Xml.Serialization.XmlAttribute("item")]
public XmlReportValue value { get; set; }
}
[Serializable]
public class XmlReportValue
{
[System.Xml.Serialization.XmlAttribute("value")]
public string count { get; set; }
}
and:
XmlReportRoot xmlRoot = new XmlReportRoot();
XmlReportItem xmlItem = new XmlReportItem();
List<XmlReportItem> itemList = new List<XmlReportItem>();
itemList.Add(xmlItem);
XmlReportValue xmlValue = new XmlReportValue();
xmlValue.count = newCustomers.ToString();
xmlItem.value = xmlValue;
xmlRoot.item = itemList;
XmlSerializer xmlSer = new XmlSerializer(typeof(XmlReportRoot));
xmlSer.Serialize(xmlRoot); //this line doesn't work
but this just feels wrong, and I couldn't quite get the serialization to work without worrying about a file stream, which I would rather do.
So I guess I was trying to find a way to do something like XmlWriter but be able to serialize that without an object type and return that, instead of having to worry about custom serializable objects types.
Use XmlWriter.Create(Response.OutputStream) and Response.ContentType = "application/xml"

XML Namespaces in ASP.NET Web API

I am currently working on a project that requires me to output XML from its endpoints along with JSON. I have the following model:
[DataContract(Namespace="http://www.yale.edu/tp/cas")]
[XmlType("serviceResponse")]
[XmlRoot(Namespace="http://www.yale.edu/tp/cas")]
public class ServiceResponse
{
[XmlElement("authenticationSuccess")]
public AuthenticationSuccess Success { get; set; }
[XmlElement("authenticationFailure")]
public AuthenticationFailure Failure { get; set; }
}
The output is as follows when success is not null:
<serviceResponse>
<authenticationSuccess />
</serviceResponse>
Now, I can see that obviously, I don't have a prefix assigned to the namespace I told the elements to be a part of. My issue is that I cannot find a place to add the namespace prefixes in MVC4 using the media formatter. I have the following in my global.asax:
GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;
GlobalConfiguration.Configuration.Formatters.XmlFormatter.RemoveSerializer(typeof(Models.ServiceResponse));
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SetSerializer(typeof(Models.ServiceResponse), new Infrastructure.NamespaceXmlSerializer(typeof(Models.ServiceResponse)));
I made a custom serializer based on XmlSerializer in an attempt to intercept the writing request and tack the namespace list on there. The issue with this method is that right now I have breakpoints inside every overrideable method and none of them are tripped when serializing which leads me to believe that my serializer isn't being used.
Is there some built in way to accomplish what I want to do or am I stuck re-implementing the XmlMediaTypeFormatter to pass in namespaces when it serializes objects?
As a followup answer: The easiest solution for me was to write my own XmlMediaTypeFormatter. As it turns out, its not nearly as intimidating as I thought.
public class NamespacedXmlMediaTypeFormatter : XmlMediaTypeFormatter
{
const string xmlType = "application/xml";
const string xmlType2 = "text/xml";
public XmlSerializerNamespaces Namespaces { get; private set; }
Dictionary<Type, XmlSerializer> Serializers { get; set; }
public NamespacedXmlMediaTypeFormatter()
: base()
{
this.Namespaces = new XmlSerializerNamespaces();
this.Serializers = new Dictionary<Type, XmlSerializer>();
}
public override Task WriteToStreamAsync(Type type, object value, System.IO.Stream writeStream, System.Net.Http.HttpContent content, System.Net.TransportContext transportContext)
{
lock (this.Serializers)
{
if (!Serializers.ContainsKey(type))
{
var serializer = new XmlSerializer(type);
//we add a new serializer for this type
this.Serializers.Add(type, serializer);
}
}
return Task.Factory.StartNew(() =>
{
XmlSerializer serializer;
lock (this.Serializers)
{
serializer = Serializers[type];
}
var xmlWriter = new XmlTextWriter(writeStream, Encoding.UTF8);
xmlWriter.Namespaces = true;
serializer.Serialize(xmlWriter, value, this.Namespaces);
});
}
}
Here is the formatter as a gist: https://gist.github.com/kcuzner/eef239003d4f99dfacea
The formatter works by exposing the XmlSerializerNamespaces that the XmlSerializer is going to use. This way I can add arbitrary namespaces as needed.
My top model looks as follows:
[XmlRoot("serviceResponse", Namespace="http://www.yale.edu/tp/cas")]
public class ServiceResponse
{
[XmlElement("authenticationSuccess")]
public CASAuthenticationSuccess Success { get; set; }
[XmlElement("authenticationFailure")]
public CASAuthenticationFailure Failure { get; set; }
}
In my Global.asax I added the following to place my formatter on the top of the list:
var xmlFormatter = new Infrastructure.NamespacedXmlMediaTypeFormatter();
xmlFormatter.Namespaces.Add("cas", "http://www.yale.edu/tp/cas");
GlobalConfiguration.Configuration.Formatters.Insert(0, xmlFormatter);
After adding the formatter and making sure my attributes were set up properly, my XML was properly namespaced.
In my case, I needed to add the cas namespace linking to http://www.yale.edu/tp/cas. For others using this, just change/replicate the Add call to your heart's content adding namespaces.

Universal conversion of types

Assuming I have a table Entities with the columns Value and Type as varchar(strings) in a database, how would I go around to loading these values in C# and converting to the type specified in the table? I was intending to save the getType result for the Type value.
Save the value.GetType().AssemblyQualifiedName in the Type column and then use Type.GetType(asmQualfName) to get the type back when loading from the db. After that you can just use Activator.CreateInstance to get an object of the correct type.
How do you read from your database?
Are you using ADO.NET, or are you using any kind of OR Mapper such as EF or NHibernate? In the case of OR Mapping you could build a strongly typed object which could define the types... the OR mapper itself is responsible for the casting.
You can do this by Activator.CreateInstance Method for sample code (which is very similar to ur case) see MSDN Sample linked above.
When you're not using any OR mapper, I would prefer to build a simple class and serialize the instance of that type into xml and store in in a xml column within the databse.
using System.Xml.Serialization;
public class MySettings
{
public String Setting1 { get; set; }
public int Setting2 { get; set; }
public String ToXml()
{
string settingsXml;
var xmlSerializer = new XmlSerializer(typeof(MySettings));
using (var stream = new MemoryStream())
{
xmlSerializer.Serialize(stream, this);
stream.Position = 0;
using(var reader = new StreamReader(stream))
{
settingsXml = reader.ReadToEnd();
}
}
return settingsXml;
}
public static MySettings FromXml(string xml)
{
MySettings settings = null;
using(MemoryStream stream = new MemoryStream(System.Text.Encoding.Default.GetBytes(xml)))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof (MySettings));
settings = (MySettings) xmlSerializer.Deserialize(stream);
}
return settings;
}
}

Error converting JSON to .Net object in asp.net

I am unable to convert JSON string to .net object in asp.net. I am sending JSON string from client to server using hidden field (by keeping the JSON object.Tostring() in hidden field and reading the hidden field value in code behind file)
Json string/ Object:
[[{"OfferId":"1","OrderValue":"11","HostingTypeID":"3"},
{"OfferId":"1","OrderValue":"11","HostingTypeID":"3"},
{"OfferId":"1","OrderValue":"11","HostingTypeID":"3"},
{"OfferId":"1","OrderValue":"2","HostingTypeID":"3"},
{"OfferId":"1","OrderValue":"2","HostingTypeID":"3"},
{"OfferId":"1","OrderValue":"67","HostingTypeID":"3"},
{"OfferId":"1","OrderValue":"67","HostingTypeID":"3"}],
[{"OfferId":"1","OrderValue":"99","HostingTypeID":"6"}],
[{"OfferId":"1","OrderValue":"10","HostingTypeID":"8"}]]
.Net Object
public class JsonFeaturedOffer
{
public string OfferId { get; set; }
public string OrderValue { get; set; }
public string HostingTypeID { get; set; }
}
Converstion code in code behind file
byte[] byteArray = Encoding.ASCII.GetBytes(HdnJsonData.Value);
MemoryStream stream = new MemoryStream(byteArray);
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(JsonFeaturedOffer));
object result= serializer.ReadObject(stream);
JsonFeaturedOffer jsonObj = result as JsonFeaturedOffer;
While converting i am getting following error:
Expecting element 'root' from namespace ''.. Encountered 'None' with name '', namespace ''.
Unfortunately, none of the proposed solutions solve the real source of the problem. This exception means that your deserializer tries to read from the end of a stream.
The solution is to rewind the stream to the beginning, ie. set the stream.Position = 0; before deserialization.
Also, as the comments mention, if you used a StreamWriter you need to flush it before using the stream.
Instead of doing this manually I would recommend using the built in lightweight JavaScriptSerializer. No attributes are required on the classes you want to serialize/deserialize.
It's also more flexible and faster than the DataContractJsonSerializer, since it does not have to care about all the wcf stuff. Additionally it has generic overloads that make it very simple to use AND it can also handle anonymous types.
Serialization:
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var objectAsJsonString = serializer.Serialize(objectToSerialize);
Deserialization:
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
JsonFeaturedOffer deserializedObject = serializer.Deserialize<JsonFeaturedOffer>(s_JsonBaseDate);
To make it even easier you can create Extension methods that will give you json serialization/deserialization directly on the objects/strings.
If you want the class to auto-magically serialize into json/xml or deserialize in the object you need to decorate it with some serializable attributes:
[Serializable, XmlRoot("JsonFeaturedOffer"), DataContract(Name="JsonFeaturedOffer")]
public class JsonFeaturedOffer
{
[XmlElement ("OfferId"), DataMember(Name="OfferId")]
public string OfferId {get; set;}
... and so on
If this is an array of arrays of JsonFeaturedOffers, shouldn't it be:
byte[] byteArray = Encoding.ASCII.GetBytes(HdnJsonData.Value);
MemoryStream stream = new MemoryStream(byteArray);
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(JsonFeaturedOffer[][]));
object result= serializer.ReadObject(stream);
JsonFeaturedOffer[][] jsonObj = result as JsonFeaturedOffer[][];

Categories

Resources