1

Is it possible when creating an anonymous type to also create aliases for the property names?

The issue I have is that my property names are rather large and I'm trying to keep the Json data to a minimum to make it more lightweight, and rather than changing the actual property names which are very descriptive, I was wondering if I can create an alias for each of the properties on the fly?

var result = myModel.Options.Select(l => new  { l.Id, l.LargePropertyName, l.LargePropertyName2 }).ToDictionary(l => l.Id.ToString(), l => new { l.LargePropertyName1, l.LargePropertyName2 });
JavaScriptSerializer serializer = new JavaScriptSerializer();
Json = serializer.Serialize(result);

Many thanks

Wildcat
  • 495
  • 2
  • 5
  • 9

1 Answers1

1

The following code snippet:

var resultWithLengthyPropertyNames = new { Identificator = 1, VeryLengthyPropertyName1 = "Fred", VeryLengthyPropertyName2 = "Brooks" };

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var sourceJSON = serializer.Serialize(resultWithLengthyPropertyNames);

System.Console.WriteLine("Source JSON => {0}", sourceJSON);  

string[] propertyNamesAliases = { "ID",  "FN", "LN"};        
var intermediateResult = (IDictionary<string, object>)serializer.DeserializeObject(sourceJSON);
var renamedIntermediateResult = new Dictionary<string, object>();
for (int i = 0; i < propertyNamesAliases.Length; i++)
    renamedIntermediateResult.Add(propertyNamesAliases[i],
        intermediateResult[intermediateResult.Keys.ToArray()[i]]);

var convertedJSON = serializer.Serialize(renamedIntermediateResult);

System.Console.WriteLine("Converted JSON => {0}", convertedJSON);  

results in the test output:

Source JSON => {"Identificator":1,"VeryLengthyPropertyName1":"Fred","VeryLengthyPropertyName2":"Brooks"}
Converted JSON => {"ID":1,"FN":"Fred","LN":"Brooks"}

Proposed solution doesn't create new anonymous object with renamed property names but it does solve your task of keeping your JSON strings lightweight, doesn't it?

P.S. Here is a second solution:

var resultWithLengthyPropertyNames = new { Identificator = 1, VeryLengthyPropertyName1 = "Fred", VeryLengthyPropertyName2 = "Brooks" };

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var sourceJSON = serializer.Serialize(resultWithLengthyPropertyNames);

System.Console.WriteLine("Source JSON => {0}", sourceJSON);

Dictionary<string, string> propertyNamesAliases = new Dictionary<string, string>() 
            {
                { "Identificator", "ID"}, 
                { "VeryLengthyPropertyName1", "FN" },
                { "VeryLengthyPropertyName2", "LN" }
            };

var renamedTempResult = new Dictionary<string, object>();

foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(resultWithLengthyPropertyNames))
{
    renamedTempResult.Add(
            propertyNamesAliases[propertyDescriptor.Name],
            propertyDescriptor.GetValue(resultWithLengthyPropertyNames));
}

var convertedJSON = serializer.Serialize(renamedTempResult);

System.Console.WriteLine("Converted JSON => {0}", convertedJSON);

P.P.S. Here is yet another solution - that seems to be solving the task directly by creating an anonymous object with aliased properties:

var resultWithLengthyPropertyNames = new { Identificator = 1, VeryLengthyPropertyName1 = "Fred", VeryLengthyPropertyName2 = "Brooks" };

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

var sourceJSON = serializer.Serialize(resultWithLengthyPropertyNames);

System.Console.WriteLine("Source JSON => {0}", sourceJSON);

var targetObjectTemplate = new { ID = -1, FN = "", LN = "" };

var convertedObject =
            Activator.CreateInstance(targetObjectTemplate.GetType(),
                    resultWithLengthyPropertyNames.GetType()
                .GetProperties()
                .Select(p => p.GetValue(resultWithLengthyPropertyNames))
                .ToArray());

var convertedJSON = serializer.Serialize(convertedObject);

System.Console.WriteLine("Converted JSON => {0}", convertedJSON);
ShamilS
  • 1,410
  • 2
  • 20
  • 40