NetJSON: Serializing anonymous objects throws an exception

Given the following models:

public struct LogEvent
{
    public DateTime Timestamp { get; set; }
    public Level Level { get; set; }
    public string Entry { get; set; }
}

public enum Level
{
    Debug,
    Trace
}

When trying to serialize the following:

var logEvents = Enumerable.Range(1, 100)
        .Select(n => new LogEvent 
        {
            Timestamp = DateTime.UtcNow,
            Level = n % 2 == 0 ? Level.Debug : Level.Trace,
            Entry = n.ToString()
        });

var anonymousObjects = logEvents
    .Select(x => 
        new {
            TimestampEpoch = x.Timestamp,
            x.Level,
            Message = x.Entry
        }
    );

var defaultSettings = new NetJSONSettings { CamelCase = true, SkipDefaultValue = false };
var json = NetJSON.NetJSON.Serialize(anonymousObjects, defaultSettings);

Throws: TypeInitializationException: The type initializer for ‘NetJSONCachedSerializer`1’ threw an exception.

Removing the defaultSettings also results in the same error.

Note that serializing logEvents does not result in any exceptions and the problem is when the result is projected to anonymous objects.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 38 (38 by maintainers)

Most upvoted comments

All the changes needed will be quite a lot expecially been able to maintain both dynamic methods and assemblies in parallel due to non public types. I have figure a way to make them pay nicely together. I will try it and see if it works.

Thanks,