0

I need to serialize an object so that the structure and naming convention are the same as what the MVC engine produces for a strongly typed view. The naming convention I am after is that which is created when using the HTML helpers.

So, for an input: <input type="text" id="Address_Street1" name="Address.Street1"/>

The property in the object would be:

public class Address{
     public string Street1 {get; set;}
}

Here is an example object:

public class Listing
    {
        public int Id { get; set; }
        public Address Address { get; set; }
    }


    public  class Address
    {
        public int Id { get; set; }
        public string Street1 { get; set; }
        public Contact Contact { get; set; }
    }

    public class Contact
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
    }

I'd like to serialize the object (assuming the Listing is the root object) so it is formatted like this and passed back to the client:

{
"Id":1,
"Address_Id":2,
"Address_Street1":"123 Lane",
"Address_Contact_Id":4
"Address_Contact_FirstName","Bill"
}

It would seem that the MVC helpers are doing this somehow and I was wondering if there are any built-in classes that take care of this type if serialization. I have tried this, but the code doe not compile and not sure what version of the serilaizer they are using. It is exactly what I am looking for.

UPDATE

I feel stupid, but it seems the answer in the reference link might contain code that is assumed to be other classes for parsing.

UPDATE 2

I came up with this, which seems to work fine. I tried to account for Nullable types in some cases. I suppose it depends if I need the property back even if it doesn't have a value. Currently, I check if a value is null, if it is, pass an empty string to the AppendToPathString method

class Program
    {
        private static void Main(string[] args)
        {
            var listing = new Listing()
                {
                    Id = 1,
                    Address = new Address()
                        {
                            Id = 1,
                            Street1 = "sdfsdfsdfsdfsd",
                            Contact = new Contact()
                                {
                                    FirstName = "ert34253453",
                                    Id = 5,
                                    Created = DateTime.Now
                                }
                        }
                };


            var json = JsonConvert.SerializeObject(listing);

            var jss = new JavaScriptSerializer();
            var o = jss.Deserialize<Dictionary<string, object>>(json);

            var additionalParameters = new Dictionary<string, string>();
            BuildVariablesList(o, "", additionalParameters);
        }



        private static string AppendToPathString(string path, object part)
        {
            return path.Trim().Length == 0 ? part.ToString() : path + '.' + part;
        }

        public static void BuildVariablesList(object obj, string path, Dictionary<string, string> result)
        {
            if (obj is ArrayList)
            {
                var arrayObj = obj as ArrayList;
                for (var i = 0; i < arrayObj.Count; i++)
                {
                    BuildVariablesList(arrayObj[i], AppendToPathString(path, i), result);
                }
            }
            else if (obj is Dictionary<string, object>)
            {
                var dictObject = obj as Dictionary<string, object>;
                foreach (var entry in dictObject)
                {

                    if (entry.Value is Dictionary<string, object>)
                    {
                        BuildVariablesList(entry.Value as Dictionary<string, object>, AppendToPathString(path, entry.Key), result);
                    }
                    else if (entry.Value is ArrayList)
                    {
                        BuildVariablesList(entry.Value as ArrayList, AppendToPathString(path, entry.Key), result);
                    }
                    else
                    {
                       if (entry.Value != null)
                       {
                           result.Add(AppendToPathString(path, entry.Key), entry.Value.ToString());
                       }
                       else
                       {
                           result.Add(AppendToPathString(path, entry.Key), string.Empty);
                       }


                    }
                }
            }

        }
    }

    public class Listing
    {
        public int Id { get; set; }
        public Address Address { get; set; }
    }


    public  class Address
    {
        public int Id { get; set; }
        public string Street1 { get; set; }
        public Contact Contact { get; set; }
    }

    public class Contact
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public DateTime Created { get; set; }
        public int? SomeId { get; set; }

    }
Community
  • 1
  • 1
DDiVita
  • 4,225
  • 5
  • 63
  • 117
  • possible duplicate of [.Net NewtonSoft Json Deserialize map to a different property name](http://stackoverflow.com/questions/15915503/net-newtonsoft-json-deserialize-map-to-a-different-property-name) – Fals Dec 08 '13 at 18:19
  • @Fals, this is in no way a duplicate of that posting. They are passing in the Json with special naming to be deserialzied later. – DDiVita Dec 08 '13 at 18:37

1 Answers1

0

So this worked perfectly for me. I added a delimiter parameter to the AppendToPathString method:

private static void Main(string[] args)
        {
            var listing = new Listing()
                {
                    Id = 1,
                    Address = new Address()
                        {
                            Id = 1,
                            Street1 = "sdfsdfsdfsdfsd",
                            Contact = new Contact()
                                {
                                    FirstName = "ert34253453",
                                    Id = 5,
                                    Created = DateTime.Now
                                }
                        }
                };


            var json = JsonConvert.SerializeObject(listing);

            var jss = new JavaScriptSerializer();
            var o = jss.Deserialize<Dictionary<string, object>>(json);

            var additionalParameters = new Dictionary<string, string>();
            BuildVariablesList(o, "", additionalParameters);
        }



        private static string AppendToPathString(string path, object part, string delimiter = ".")
        {
            return path.Trim().Length == 0 ? part.ToString() : path + delimiter  + part;
        }

        public static void BuildVariablesList(object obj, string path, Dictionary<string, string> result)
        {
            if (obj is ArrayList)
            {
                var arrayObj = obj as ArrayList;
                for (var i = 0; i < arrayObj.Count; i++)
                {
                    BuildVariablesList(arrayObj[i], AppendToPathString(path, i), result);
                }
            }
            else if (obj is Dictionary<string, object>)
            {
                var dictObject = obj as Dictionary<string, object>;
                foreach (var entry in dictObject)
                {

                    if (entry.Value is Dictionary<string, object>)
                    {
                        BuildVariablesList(entry.Value as Dictionary<string, object>, AppendToPathString(path, entry.Key), result);
                    }
                    else if (entry.Value is ArrayList)
                    {
                        BuildVariablesList(entry.Value as ArrayList, AppendToPathString(path, entry.Key), result);
                    }
                    else
                    {
                       if (entry.Value != null)
                       {
                           result.Add(AppendToPathString(path, entry.Key), entry.Value.ToString());
                       }
                       else
                       {
                           result.Add(AppendToPathString(path, entry.Key), string.Empty);
                       }


                    }
                }
            }

        }
DDiVita
  • 4,225
  • 5
  • 63
  • 117