doorman
doorman

Reputation: 16949

Deserializing string using ServiceStack

I am trying to deserialize a simple json string using ServiceStack to Dto object:

public class TestDto
{
    public int MyProp { get; set; }
}

var json = JsonSerializer.DeserializeFromString<TestDto>("{\"MyProp\":6}");

I get the exception:

[System.TypeInitializationException] = 
{"The type initializer for 'ServiceStack.Text.Json.JsonReader`1' threw an exception."}

// With the inner exception:
"Object reference not set to an instance of an object."

// And the stack trace:
at ServiceStack.Text.Common.JsReader`1.GetCoreParseFn[T]()
at ServiceStack.Text.Common.JsReader`1.GetParseFn[T]()
at ServiceStack.Text.Json.JsonReader`1..cctor()

I am developing in Visual Studio using Xamarin on Windows 8.1. The ServiceStack code is located inside a portable library. At the moment I am triggering the portable library code directly from a unit test.

Here is the Nuget package info for ServiceStack:

<package id="ServiceStack.Text" version="4.0.36" 
targetFramework="portable-net45+win+MonoAndroid10+xamarinios10+MonoTouch10" />

Any idea what I am missing here?

Upvotes: 2

Views: 553

Answers (1)

mythz
mythz

Reputation: 143319

The docs for using the PCL clients and Json Serializer is in the HelloMobile project, specifically you need to install the ServiceStack.Client NuGet package which contains the platform specific extensions required to support the platform you're running on:

PM> Install-Package ServiceStack.Client

For iOS you should also explicitly initialize the PCL library with:

IosPclExportClient.Configure();

Upvotes: 3

Related Questions