2012-02-13 5 views
1

Est-il possible lors de la création d'un type anonyme de créer également des alias pour les noms de propriétés? Le problème que j'ai est que mes noms de propriété sont plutôt grands et j'essaye de garder les données de Json au minimum pour le rendre plus léger, et plutôt que de changer les noms réels de propriété qui sont très descriptifs, j'étais Je me demande si je peux créer un alias pour chacune des propriétés à la volée?Alias ​​pour les propriétés de type anonyme

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); 

Un grand merci

Répondre

0

L'extrait de code suivant:

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); 

résultats dans la sortie de test:

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

solution proposée ne crée pas de nouvel objet anonyme à la propriété renomme noms mais cela résout votre tâche de garder vos chaînes JSON légères, n'est-ce pas?

P.S. Voici une seconde 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. Voici encore une autre solution - qui semble résoudre directement la tâche en créant un objet anonyme avec des propriétés aliasées:

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); 
Questions connexes