1

I have a class which i want to serialize

public class DesignEntity
{
    public string DesignName { get; set; }
    public string DesignID { get; set; }
    [ScriptIgnore]
    public string designDetails { get; set; }
}

property designDetails already contains serialized data so no need to serialized it again

DesignEntity designSessionEntity = null;
JavaScriptSerializer jSerializer = null;
string designJsonSession = "null";

designSessionEntity = new DesignEntity();
jSerializer = new JavaScriptSerializer();

designSessionEntity = /* */ // Get Values via some method
designJsonSession = jSerializer.Serialize(designSessionEntity); 
// Now append designDetails to resultant string json

I want to append designDetails to designJsonSession after serialization is complete. How can i achieve the same ?

7
  • Deserialize, add and serialize again. Commented Dec 7, 2015 at 7:58
  • Show the initial format, and the desired result. It is pretty unclear what output format you want. Commented Dec 7, 2015 at 7:59
  • Why are u doing this: string designJsonSession = "null";? You can just do string designJsonSession = null; Commented Dec 7, 2015 at 7:59
  • @Sybren There is actually no need to assign null - it is default value. Moreover, there is no need to split declaration and assignment up :) Commented Dec 7, 2015 at 8:00
  • @YeldarKurmangaliyev I know there's no need to. But looking at his code he want's to assign null. Commented Dec 7, 2015 at 8:05

1 Answer 1

3

First, add one more field to your DesignEntity class. Whose type is the contract class that designDetails json is serialized from.

public class DesignEntity
{
    public string DesignName { get; set; }
    public string DesignID { get; set; }
    /// new field
    public DesignDetailsObject DesignDetails { get; set; }
    [ScriptIgnore]
    public string designDetails { get; set; }
}

And then, get your object as usual, deserialize the json first into that new field, then serialize it.

var designSessionEntity = new DesignEntity();
var jSerializer = new JavaScriptSerializer();

designSessionEntity = /// Get Values via some method

/// assign the design details with deserialized object
designSessionEntity.DesignDetails = jSerializer.Deserialize<DesignDetailsObject>(designSessionEntity.designDetails);

/// serialize it again
var designJsonSession = jSerializer.Serialize(designSessionEntity); 

Voila!

Update

If you don't have access to the class that designDetails json is serialized from, then you have two options.

  1. Try to deduce the class from the json structure and create the DesignDetailsObject yourself (Recommended).
  2. Append the json string manually (I'll try to show you how if you need) - This method must be applied if the class is really heavy, and won't worth the work.

Code Snippet For option 2:

public string JsonAppender ( string targetJson, List<string> fields, object value )
{
    var insertIndex = 0;
    foreach ( var field in fields )
    {
        var _fieldDescriptor = field + "\":";
        insertIndex += targetJson.Substring(insertIndex).IndexOf(_fieldDescriptor) + _fieldDescriptor.Length;
    }

    var lengthOfDefaultVal = targetJson.Substring(insertIndex).IndexOf("\"") - 1;

    return targetJson.Substring(0, insertIndex) + "\"" + value + "\"" + targetJson.Substring(insertIndex + lengthOfDefaultVal);
}

Usage:

var fieldAppendedJson = JsonAppender(json, new List<string> { "designDetails " }, designSessionEntity.designDetails);
Sign up to request clarification or add additional context in comments.

2 Comments

What is DesignDetailsObject ?
That is supposed to be the class that your json designDetails is serialized from at the first place.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.