0

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

The JsonData instance has to be initialized first

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);
        }
    }
}
Segfault
  • 8,036
  • 3
  • 35
  • 54
  • 1
    PowerShell 3 has JSON support with ConvertFrom-Json and ConvertTo-Json. See http://stackoverflow.com/questions/16575419/powershell-retrieve-json-object-by-field-value – Lars Truijens May 28 '13 at 19:51

1 Answers1

1

That would totally break .Net. I doubt that. Even more since the error is from the JsonData class and is because a type field is not set from the (non static) constructor of that class. See http://couchbrowse.googlecode.com/svn/trunk/LitJson/JsonData.cs Are you sure your LitJson code is correct? Does this exact code work in a C# program?

UPDATE

Are you sure your Json is correct? The example you posted isn't. There should be double quotes. Try escaping use the back tick or use a here-string

[litjson.jsonmapper]::toobject("{`"foo`":`"bar`"}")
Lars Truijens
  • 42,837
  • 6
  • 126
  • 143