0

How do I do the equivalent in JSON.net?

 public SerializedResults SerializeResults(Type queryType, IEnumerable entities)
      {
          var results = SerializeDynamicType(queryType);

          var objList = AnonymousFns.DeconstructMany(entities, false, queryType).ToList();

          var ms = new MemoryStream();
          var type = objList.GetType();
          var serializer = new DataContractSerializer(type);

          using (ms)
          {
              using (GZipStream compress = new GZipStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression))
              {
                  serializer.WriteObject(compress, objList);
              }
          }

          results.ByteArray = ms.ToArray();

          return results;
      }

I am confused with this line in particular: var serializer = new DataContractSerializer(type);

How do you do that in JSON.NET??

THANKS :-)

2 Answers 2

1

With JSON.NET, you don't need the type when serializing. I'm assuming that it works out the type you are passing in on its own.

So, you can get rid of this completely:

var type = objList.GetType();
var serializer = new DataContractSerializer(type);

And change this:

serializer.WriteObject(compress, objList);

To:

var json = JsonConvert.SerializeObject(objList);

Here are the JSON.Net docs for JsonConvert.

Sign up to request clarification or add additional context in comments.

3 Comments

You can pass the json string to the stream, but of course it won't be a streamed operation and instead you'll have the entire json string in memory. Not a problem unless the object is very large.
@AaronLS yes that is right. I decided to focus on the point the OP made about not understanding the type parameter.
Yeh probably not a big deal either way, since in the end they are doing ToArray, so it all ends up in memory anyways.
0

I believe you can use the BsonWriter to write to a stream. I'm not sure it will give you the exact same binary format you had before, but in concept it is the same.

  public SerializedResults SerializeResults(Type queryType, IEnumerable entities)
  {
      var results = SerializeDynamicType(queryType);

      var objList = AnonymousFns.DeconstructMany(entities, false, queryType).ToList();

      var ms = new MemoryStream();         

      using (ms)
      {
          using (GZipStream compress = new GZipStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression))
          {
            using( BsonWriter writer = new BsonWriter(compress))
            {
              JsonSerializer serializer = new JsonSerializer();                 
              serializer.Serialize(writer, objList);
            }
          }
      }

      results.ByteArray = ms.ToArray();

      return results;
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.