0

I am working on a WPF application. In this application we are having a “Sync” functionality. That is user can sync the LOCAL state to Server, or sync the LOCAL state from SERVER.

The current structure of state file is as follows

[Serializable]
public class State
{
    [XmlAttribute("Priority")]
    public int Priority { get; set; }

    [XmlAttribute("Name")]
    public string Name { get; set; }

    [XmlElement("Auto")]
    [XmlElementAttribute("AMPMConstriants", typeof(AMPMConstriants))]
    [XmlElementAttribute("CxDays", typeof(CxDays))]
   
    public object Object { get; set; }

    

}

The purpose of “XmlAttribute” is , we are using xmlSerialization to save the state object locally as a file.

For sync purpose we are converting the state object as “json” string using NewtonSOft.Json. You can see that we are using data type “Object”, this is because we need to hold different types object in to the same object. That is “object” value may “AMPMConstraints” type or it may CxDays type.

But the issue is when we serializing the state object is not properly serializing for “Object” data type. So naturally we are not getting the value of “Object” when we deserializing json string.

We had researched this issue and found a solution like add “JsonSerializerSettings”. You can see it in the below image.

 private static string JsonObjectToStringSerializer<T>(T t)
        {
            string result = string.Empty;
            var jsonSerializerSettings = new JsonSerializerSettings()
            {
                TypeNameHandling = TypeNameHandling.Auto,
                            
            };
            result = JsonConvert.SerializeObject(t,  jsonSerializerSettings);
           
            return result;

        }

This solution worked fine and now we are able to serialize state object to json string and deserialize the json string to state object. But still we are facing an issue related with name space .

enter image description here

The above is the json string generated when serializing. You can see that it is showing full namespace. How do I remove the full name space from this json string.

Please note that I am using TypeNameHandling = TypeNameHandling.Auto,

The following are the class structure for AMPMConstraints and CXDays

 [Serializable]
    public class AMPMConstriants
    {
        [XmlAttribute("AM")]
        public bool AM { get; set; }
        [XmlAttribute("PM")]
        public bool PM { get; set; }
      
    }
   [Serializable]
    public class CxDays
    {
        [XmlAttribute("IsSun")]
        public bool IsSun { get; set; }

        [XmlAttribute("IsMon")]
        public bool IsMon { get; set; }

        [XmlAttribute("IsTue")]
        public bool IsTue { get; set; }

        [XmlAttribute("IsWed")]
        public bool IsWed { get; set; }

        [XmlAttribute("IsThu")]
        public bool IsThu { get; set; }

        [XmlAttribute("IsFri")]
        public bool IsFri { get; set; }

        [XmlAttribute("IsSat")]
        public bool IsSat { get; set; }
    }

We are stuck with this issue. If you have any idea on this issue please let us know. It will be helpful to us.

Thanks

Community
  • 1
  • 1
Ranish
  • 877
  • 2
  • 10
  • 30
  • 1
    For code, please include the real code as text rather than image. – Arghya C Jan 04 '16 at 05:13
  • I have replaced the image and added actual code. – Ranish Jan 04 '16 at 05:19
  • Looks like a duplicate of [Json serialization for Object Data Type](https://stackoverflow.com/questions/34576078/json-serialization-for-object-data-type/34581537) – dbc Jan 04 '16 at 14:14

0 Answers0