Avoid the root element while serializing Dictionary with DataContractJsonSerializer

46 Views Asked by At

I have a Dictionary like this which I want to serialize in Json:

xrmParentPluginContextProperties = new Dictionary<string, string> {
       { "source", className },
       { "correlationId", executionContext.ParentContext.CorrelationId.ToString() },
       { "depth", executionContext.ParentContext.Depth.ToString() },
       { "initiatingUserId", executionContext.ParentContext.InitiatingUserId.ToString() },
       { "isInTransaction", executionContext.ParentContext.IsInTransaction.ToString() },
       { "isolationMode", executionContext.ParentContext.IsolationMode.ToString() },
       { "message", executionContext.ParentContext.MessageName },
       { "mode", getModeName(executionContext.ParentContext.Mode) },
       { "operationId", executionContext.ParentContext.OperationId.ToString() },
       { "orgId", executionContext.ParentContext.OrganizationId.ToString() },
       { "orgName", executionContext.ParentContext.OrganizationName },
       { "requestId", executionContext.ParentContext.RequestId.ToString() },
       { "userId", executionContext.ParentContext.UserId.ToString() },
       { "entityId", executionContext.ParentContext.PrimaryEntityId.ToString() },
       { "entityName", executionContext.ParentContext.PrimaryEntityName },
       { "type", "Plugin" },
       { "stage", getStageName(executionContext.Stage) }
};

Then I have a helper class holding the dictionary for serialization:

public class DictionarySerializationHelperClass
{
    public Dictionary<string, string> dictionary;

    public DictionarySerializationHelperClass()
    {
    }

    public DictionarySerializationHelperClass(Dictionary<string, string> dict)
    {
        this.dictionary = dict;
    }
}

I call the serialization like this:

DictionarySerializationHelperClass dictionaryClass = new DictionarySerializationHelperClass(XrmProperties.currentPluginContext);
return SerializationHelper.JsonSerialize(dictionaryClass);

And the serialization itself:

public static string JsonSerialize<T>(T objectToSerialize) where T : class, new()
        {
            string serialisedJson = null;
            DataContractJsonSerializer serializer;
            try
            {
                using (MemoryStream serializationStream = new MemoryStream())
                {
                    if (typeof(T) == typeof(Dictionary<string, string>))
                    {
                        serializer = new DataContractJsonSerializer(typeof(T), new DataContractJsonSerializerSettings()
                        {
                            UseSimpleDictionaryFormat = true,
                            RootName = string.Empty
                        });
                    }
                    else
                    {
                        serializer = new DataContractJsonSerializer(typeof(T));
                    }
                    
                    serializer.WriteObject(serializationStream, objectToSerialize);
                    serializationStream.Position = 0;
                    var reader = new StreamReader(serializationStream);
                    serialisedJson = reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                string error = $"Error on Serializing JSON: \r\n\r\n{objectToSerialize}";

                throw new JsonException(error, ex);
            }
            return serialisedJson;
        }

The problem with the result is that I always get the following json which includes the root element "dictionary":

{
   "dictionary":{
      "source":"asdasdasd",
      "correlationId":"asdasdasd",
      "depth":"1",
      "initiatingUserId":"asdasdasd",
      "isInTransaction":"False",
      "isolationMode":"2",
      "message":"Retrieve",
      "mode":"Synchronus",
      "operationId":"asdasd",
      "orgId":"esdfsdf",
      "orgName":"asdasdasd",
      "requestId":"asdasdasd",
      "userId":"asdasdasd",
      "entityId":"asdasdasd",
      "entityName":"incident",
      "type":"Plugin",
      "stage":"Pre-operation"
   }
}

How can I remove this root element to have finally the json looking like this?

{
  "source":"asdasdasd",
  "correlationId":"asdasdasd",
  "depth":"1",
  "initiatingUserId":"asdasdasd",
  "isInTransaction":"False",
  "isolationMode":"2",
  "message":"Retrieve",
  "mode":"Synchronus",
  "operationId":"asdasd",
  "orgId":"esdfsdf",
  "orgName":"asdasdasd",
  "requestId":"asdasdasd",
  "userId":"asdasdasd",
  "entityId":"asdasdasd",
  "entityName":"incident",
  "type":"Plugin",
  "stage":"Pre-operation"
}

I've tried to set

Root = string.Empty

in the DataContractJsonSerializerSettings but it seems not to help.

Any hint is highly appreciated.

0

There are 0 best solutions below