C# - Serialize An Object In Javascript, Then Deserialize It?
Oct 1, 2010
I have a custom Javascript object that has a few string and float members. I'd like to serialize an array of them in Javascript, assign the result to a hidden field, and then retrieve and deserialize them in the codebehind for my asp.net application.
i need to serialize and deserialize an image for my project. i have been googling it for a few days but i cannot find any VB codes related to image serialization... can someone give me some hints on how i should start?? for my program, i need to save the image to sql database and then i need to retrieve all the image for my product catalog
I'm trying to save a custom class in a session, but it just never gets saved, along with other sessions. Note that my SessionMode = STATESERVER, and i cannot change it to INPROC as per our business requirement. This is my class
/*******START*******/ [Serializable] public class User : ISerializable { private int _userId; public int UserId { get { return _userId; } set { _userId = value; } } //custom class private TDB.tdbUser _tdbUserDetails; public TDB.tdbUser tdbUserDetails { get { return _tdbUserDetails; } set { _tdbUserDetails = value; } } //collection of custom class Sites private List<Sites> _assignedSites; public List<Sites> AssignedSites { get { return _assignedSites; } set { _assignedSites = value; } } public User() { //Constructor code } /******SERIALIZATION*****/ protected User(SerializationInfo info, StreamingContext context) { this._userId = (int)info.GetValue("_userId", typeof(int)); this._assignedSites = (List<Sites>)info.GetValue("_assignedSites", typeof(List<Sites>)); this._tdbUserDetails = (TDB.tdbUser)info.GetValue("_tdbUserDetails", typeof(TDB.tdbUser)); } [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("_userId", this._userId); info.AddValue("_assignedSites", this._assignedSites); info.AddValue("_tdbUserDetails", this._tdbUserDetails); } } /*******END*******/
The calling code is just this: (User)HttpContext.Current.Session["TDBUSER"];
From VB.net code behind when I am trying to deserialize it it's stating that
"Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''."
Deserialization code snippet:
Dim serializer = New DataContractJsonSerializer(GetType(List(Of Employee))) Dim memoryStream = New MemoryStream() Dim s = msg.Content.ReadAsString() serializer.WriteObject(memoryStream, s) memoryStream.Position = 0.......
public class AccessToken { public string access_token {get;set;} public DateTime expires { get; set; } } public class TokenReturn { public List<AccessToken> tokens { get; set; } } JavaScriptSerializer ser = new JavaScriptSerializer(); TokenReturn result = ser.Deserialize<TokenReturn>(responseFromServer);
I've been struggling with this issue for a while. I'm trying to send an object to the client. Once the client updates the object I'd like to post it back to the server and save it in my database. The first part works fine. However, when I send it back all the dates are messed up and start for year 0001. I assume that's because it can't deserialize the date. Howe can I post a json object with a date property and deserialize it into a type on the server with asp.net mvc 3? group is
public class Group { public Group(); public string CreatedBy { get; set; } public DateTime CreatedOn { get; set; } public string Description { get; set; } public string Name{ get; set; } public string Status { get; set; } public string UpdatedBy { get; set; } public DateTime UpdatedOn { get; set; } } while public JsonResult updateGroup(Group group) { var result = Repository.updateGroup(group); return Json(result, JsonRequestBehavior.AllowGet); }
I had an xml-serialization which worked fine, but as I have just knew it doesn't work in Opera browser. I think, it's security rules don't allow to write to cookies xml-content.What can I do? Can I use binary serialization or something else?
I would like to serialize the properties of the HttpBrowserCapibilities object so that it may be returned via a web method call. Currently the object cannot be serialized:Cannot serialize member System.Web.Configuration.HttpCapabilitiesBase.Capabilities of type System.Collections.IDictionary, because it implements IDictionary. ...which is understandable. However, I would like to simply copy out the properties and their values to a hierarchy, i.e.
I'm starting to think I would need to use reflection to copy this object, but I haven't reached a conclusion. Does anyone have any suggestions to keep this simple?
I'm in the process of deserializing a JSON object into an Entity Framework object. The property names in the JSON do not match up totally with the property names in the Entity Framework Objects. What I'd like to do is set "Name=" on the "DataContractAttribute" for the table and the "DataMemberAttribute" for the properties in the Designer.cs file. I tried setting these in the Designer.cs file manually just to see if it would work - which it did (using JSON.Net) but of course when I went into the .edmx designer I lost all my changes. Is there a way to do this?
Let's say I have a function that takes a string. That string contains the fully name of an enum type (e.g. "MyCompany.Area.AotherNamespace.MyEnum").
How could I create an array of strings (or List<string>) whose elements are the values of MyCompany.Area.AotherNamespace.MyEnum?
Is that even possible?I'm basically trying to serialize an enum type serverside and then output it in clientside JavaScript so I don't have to define an enum in two places--in my C# and my JavaScript.
public class Package { public Package() { name = ""; type = new List<Dictionary<string, string>>(); } public string name { get; set; } public List<Dictionary<string, string>> type { get; set; } } [code]...
I have embed the fullcalender control in my asp.net mvc application. It is running fine locally. but when I uploads it to my domain server (third party) it showing me This Error: Uncaught TypeError: Object # has no method 'fullCalendar' in crome console (debugger). and not rendering the control. ** EDITED: My HTML code is this **
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> Index <% var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); %> < style type='text/css'> body { margin-top: 40px; text-align: center; font-size: 14px; font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif; }..............................................
to be able to send a XmlSerializer class (which is generated obvious in remote C# application) over a WebService that will then deserialize it into a class. (I didnt know it its possible either) XmlSerializer mySerializer = new XmlSerializer(typeof(SystemInfo)); StreamWriter myWriter = new StreamWriter(textBox1.Text); mySerializer.Serialize(myWriter, sysinfo); [WebMethod] public void Reports(XmlSerializer xmlSerializer)
I am using Json.Net however when I go to deserialize the following json I get that Json cannot deserialize to type List.
Json:
{"postalCodes":[{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10001","countryCode":"US","lng":-73.996705,"placeName":"New York City","lat":40.74838,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10019","countryCode":"US","lng":-73.985834,"placeName":"New York City","lat":40.765069,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10021","countryCode":"US","lng":-73.958805,"placeName":"New York City","lat":40.768476,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10022","countryCode":"US","lng":-73.965703,"placeName":"New York City","lat":40.757091,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10036","countryCode":"US","lng":-73.991826,"placeName":"New York City","lat":40.759724,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10065","countryCode":"US","lng":-73.96379,"placeName":"New York City","lat":40.76507,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10003","countryCode":"US","lng":-73.989223,"placeName":"New York City","lat":40.731253,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10010","countryCode":"US","lng":-73.981328,"placeName":"New York City","lat":40.737476,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10011","countryCode":"US","lng":-73.99963,"placeName":"New York City","lat":40.740225,"adminName1":"New York"},{"adminName2":"New York","adminCode2":"061","adminCode1":"NY","postalCode":"10012","countryCode":"US","lng":-73.998284,"placeName":"New York City","lat":40.72553,"adminName1":"New York"}]}
I have a simple class in asp.net mvc that looks like this:
[code]...
It deserializes it so I have to reference everything by index first, because of the list. So if I had a property called "IsValid" I would have to reference it like this "IsValid[0]". I have way too much javascript code to make these changes.How could I deserialize the JsonResponseItem class so I don't need the index reference in there?
[ServiceContract] public interface IBusiness { [OperationContract][code]....
Add client: [Code]....
But......... the result
Error:The server encountered an error processing the request. The exception message is 'The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'UpdateLanguage'. The token '"' was expected but found '''.'. See server logs for more details. The exception stack trace is:
at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest
Im using DataTable to write some XML. but i got Serialization problem. when i try write i got error message, your datatable is not serialize.(i dont want use DataSet)How i Serialize DataTable?
I'm trying to serialize a Request object for logging purposes. The code
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType()); // obj is a Request object
gives me the following exception:To be XML serializable, types which inherit from ICollection must have an implementation of Add(System.String) at all levels of their inheritance hierarchy. System.Web.HttpValueCollection does not implement Add(System.String).