I'm trying to use Leonardo B's litjson library from PowerShell. The entry point to the library I need to use is a static method. I can load the assembly with add-type, no problem. Trying to use the static method though gives this error:
PS C:\Users\david>[litjson.jsonmapper]::toobject("{`"foo`":`"bar`"}")
format-default : The JsonData instance has to be initialized first
+ CategoryInfo : NotSpecified: (:) [format-default], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.FormatDefaultCommand
I though that what was happening was that the static constructor for this class didn't run, but Lars Truijens corrected that misunderstanding. The error is an exception from the JsonData class. Although the same line of code works correctly in VS2012 (targeting .net 3.5) it won't run in powershell.
UPDATE:
This unit test passes in visual studio 2012:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using LitJson;
namespace litjsonTest
{
[TestClass]
public class TestJsonMapper
{
[TestMethod]
public void TestMethod1()
{
var obj = LitJson.JsonMapper.ToObject("{\"foo\":\"bar\"}");
Assert.IsNotNull(obj);
}
}
}