neuecc / Utf8Json

Definitely Fastest and Zero Allocation JSON Serializer for C#(NET, .NET Core, Unity, Xamarin).
MIT License
2.35k stars 266 forks source link

JsonSerializer.ToJsonString throws when one of the property of the object throws #159

Open Pvlerick opened 5 years ago

Pvlerick commented 5 years ago
 [Fact]
public void Test()
{
    var value = new MyObject();

    //throws
    Utf8Json.JsonSerializer.ToJsonString(value);
}

public class MyObject
{
    public string Value => throw new Exception();
}

Is there any way to customize the behaviour of the serializer so that this doesn't throw? For example, e.Message could be set as the value for that property.

Tornhoof commented 4 years ago

If you don't want to use [IgnoreDataMember] to completely ignore the member, you can probably use the ShouldSerializePattern for that: https://github.com/neuecc/Utf8Json#shouldserializexxx-pattern And in the ShouldSerializeValue method check if it throws an exception and return true/false appropriately.

Pvlerick commented 4 years ago

Well, the idea is not to have to mess with the objects that I want to serialize.

It's a bit of a deviated use of this library, but I'd like to use with NLog for structured logging, and in this case I don't know in advance which are the object that are going to be serialized.