facebook-csharp-sdk / simple-json

JSON library for .NET 2.0+/SL4+/WP7/WindowsStore with optional support for dynamic and DataContract
MIT License
379 stars 143 forks source link

Open to new serializer features? #52

Open ejsmith opened 10 years ago

ejsmith commented 10 years ago

I would like to implement the ability to do the following things while serializing objects:

  1. Exclude fields that are using default values. I know some people will want to include these fields, but for my use case I would rather only serialize the minimum about of data necessary.
  2. Exclude empty collections. Same reasoning as above.
  3. Add ability to set maximum serialization depth. I would like to have the ability to set a maximum object depth for serialization in order to avoid serializing all of a really large object graph. This is because I am attempting to serialize unknown objects that I do not have control over.
  4. Add ability to set an exclusion list of fields that I do not want serialized.

These seem like general purpose features, but I also understand that you don't want to have feature bloat. Would you guys be open to accepting a pull request like this?

prabirshrestha commented 10 years ago

All the features are pretty interesting and seems like good to have in SimpleJson but I also don't want to increase the complexity of SimpleJson.

Out of curiosity I would want to know what you are using SimpeJson for (client/server or any other details)?

ejsmith commented 10 years ago

I am using it to serialize random unknown objects in our Exceptionless client. I need to be able to allow people to set exclusions and also to set serialization depth.

I started taking a look at implementing this, but, because the entire code base is static, there doesn't appear to be a way to keep track of the current serialization depth without making a decent amount of changes.

prabirshrestha commented 10 years ago

@ejsmith totally makes sense to add SerializationDepth in your case.

This was originally a fork from a different json parser library written in .NET 1.0 using hashtables and arraylist without generics, so definitely needs a lot of work on the api surface. Also one of the main reason why SimpleJson hasn't made it to v1 release is due to fact that I haven't been happy with the public api yet.

I do think IJsonSerializerStrategy can solve the problem but the interface methods aren't good enough yet. It was a fast hack so that I could get fb c# sdk to internally use SimpleJson.

interface IJsonSerializerStrategy {
    int SerializationDepth { get; }
    bool TrySerializeNonPrimitiveObject(object input, out object output);
    object DeserializeObject(object value, Type type);
}

I'm very much open to this interface being changed even though it would cause breaking change. (one reason why SimpleJson is still in alpha :smiley: )

this also means we need to add MaxDepthSerializationException which inherits SerializationException.

Feel free to post your thoughts on the interface.

sicklittlemonkey commented 8 years ago

I added pretty printing in about 25 lines. It would be nice if that was a serialization option too.