ServiceStack ORMLite not deserializing JSON stored

2019-07-11 10:13发布

问题:

I have some telemetry data that was stored in a text field as JSON. I'm attempting to reverse-engineer POCOs to seamlessly extract and present that data without having to do any post-processing ForEach loops.

When I attempt to manually parse the JSON string in the data field, it works. When I am selecting via ORMLite, it comes back with the default object. What am I missing?

Works

string x = "{\"alive\":true,\"paperStatus\":\"0:NoIssue\"}";
var telemetry = JsonSerializer.DeserializeFromString<KioskTelemetryData>(x);

Doesn't populate Data field

var exp = Db.From<KioskTelemetryLog>()
                    .Where(q => q.Identifier == request.Identifier)
                    .OrderByDescending(q => q.Timestamp);

var data = Db.Select(exp);

Here is what the data records look like:

Id  Identifier  IsCurrent   RedemptionCenterId  Timestamp               Status  Data
1   XXX        0            NULL                2015-11-24 11:10:53.527 1       {"alive":true,"paperStatus":"1:LowPaper"}
2   XXX        0            NULL                2015-12-01 12:16:56.653 0       {"alive":true,"paperStatus":"0:NoIssue"}    
1   XXX        1            NULL                2015-12-01 18:32:11.337 2       {"alive":false}

And here are the POCOs:

[Alias("TelemetryLog")]
public class KioskTelemetryLog
{
    public long Id { get; set; }

    public string Identifier { get; set; }

    public bool IsCurrent { get; set; }

    public int? RedemptionCenterId { get; set; }

    public DateTime Timestamp { get; set; }

    // 0 = okay, 1 = warning, 2 = error
    public sbyte Status { get; set; }

    public KioskTelemetryData Data { get; set; }
}

public class KioskTelemetryData
{
    public bool Alive { get; set; }
    public string PaperStatus { get; set; }
}

回答1:

The default complex type serializer for OrmLite is the JSV Format (except for PostgreSQL which uses JSON). OrmLite does support pluggable text serializers, e.g. you can change SQL Server to use JSON for serializing complex types with:

SqlServerDialect.Provider.StringSerializer = new JsonStringSerializer();

If it's not serializing correctly it's like a serialization error, you can try enable logging for more info about the error, e.g:

LogManager.LogFactory = new ConsoleLogFactory(debugEnabled:true);