.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

DataContractJsonSerializer "Encountered unexpected namespace"

Posted By:      Posted Date: September 20, 2010    Points: 0   Category :WCF

I'm getting an error and there are 0 (ZERO) hits on google. This is a bad sign.

here's the code

    public static string ToJson(this object obj)
      DataContractJsonSerializer serializer = new DataContractJsonSerializer( obj.GetType() );

      MemoryStream stream = new MemoryStream();
      serializer.WriteObject( stream, obj );

        return Encoding.UTF8.GetString( stream.ToArray() );

the object that i'm serializing has [DataContract(Namespace="...")] since this is coming from a WCF service.

This was working a moment ago. Without search hits on the error message i'm pretty stuck.

Please help.


View Complete Post

More Related Resource Links

DataContractJsonSerializer: Problem when deserializing the Date

Sorry for cross-posting. I posted the same question on the Silverlight forum, but it has not received a single answer yet. The majority of folks there must be more interested in UI than in more fundamental stuff.     Hello everyone! Let's say we have the folllowing POCO classes: public class PocoObject : List<PocoPropertyData> {     public PocoObject() {}     public override string ToString()     {         var parts = new List<string>();         foreach (var pd in this)             parts.Add(string.Format("{{N=[{0}], Type=[{1}], V=[{2}]}}", pd.N, pd.V == null ? typeof(object) : pd.V.GetType(),  pd.V));         return string.Join(",", parts);     } } public class PocoPropertyData {     public string N { get; set; }  // the property name     public object V { get; set; }  // the property value } As you can see, the PocoPropertyData can hold value of any type. When I execute the following code in both .NET 4 application and Silverlight 4 application: ========================================================= var poco = new PocoObject {       { new PocoPropertyData { N = "DateTimeField", V =  Da

DataContractJsonSerializer changing my date

A user enters a date in a form, but when I pass the date through an AJAX json call, my date is getting changed by the DataContractJsonSerializer. Here's my AJAX call:function Save(){ //Convert date to milliseconds from from 1/1/1970 - necessary for passing a date through JSON var d = "09/01/2010 05:00"; myDate = new Date(d); myTicks = myDate.getTime(); var message = { MessageID: $("#MessageID").val(), MessageDate: '\/Date(' + myTicks + ')\/', }; $.ajax({ url: "<%= Url.Action("SaveMessage") %>", type: "POST", dataType: "json", data: $.toJSON(message), contentType: "application/json; charset=utf-8", success: function(result) { if (result && result.success) { // } } }); } Here's my serialization code:public override void OnActionExecuting(ActionExecutingContext filterContext){ if ((filterContext.HttpContext.Request.ContentType ?? string.Empty).Contains("application/json")) { var serializer = new DataContractJsonSerializer(RootType); filterContext.ActionParameters[Param] = serializer.ReadObject(filterContext.HttpContext.Request.InputStream); }} It's changing my dates to GMT. So if the user ente

DataContractJsonSerializer serialization of EnumMember

I discovered the other day that the DataContractJsonSerializer ignores the [EnumMember] attribute.  If it encounters a enum value it will always use the integer value of the enum.  For example if I define the following data contracts... [DataContract] public enum VehicleType { [EnumMember(Value = "Car")] Car, [EnumMember(Value = "Bike")] Bike } [DataContract] public class MyDataContract { public VehicleType Vehicle { get; set; } } ... and return a MyDataContract with Vehicle set to VehicleType.Bike it will be serialised in Json as 1.  If I use the standard Xml serialiser it will be serialised to my specified value of 'Bike'. This is specified in this MSDN article as the intended behaviour, but it seems a bit strange to me and a little bit frustrating.  I could probably spend time writing my own serialiser to do this, but it seems like a feature that should be there in the framework.   Is there a good reason why this serialiser ignores the [EnumMember] attributes?  If not could this be changed in a future release? Thanks IainBlog Twitter Please "mark as answer" if this answers the question.. thanks!

Using DataContractJsonSerializer on dynamic JSON object


I'm using the DataContractJsonSerializer and the DataContract and DataMember class atrributes to serialize some JSON sent to me from a 3rd party javascript component. All is working well except the component sends some dynamic data.


ex. { "objects": [{"object1": "value"}, {"SomeOtherObject": "anotherValue"}] ... }


What are my options for serializing this if the "object1", "SomeOtherObject" are all dynamic and I can add them into the class that I serialize to. Is it possible to get the JSON Serializer to convert this to a Dictionary<string, string>??


Also I'm using C# 3.0 so dynamics is out.

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend