アリスは System.Json に出会う

ということで、さっそくダウンロードして、無理矢理 .NET Framework のほうでビルドします。ええ、.NET Core じゃなくて、普通の .NET Framework に持って来るわけですね。

corefx/src/System.Json at master ・ dotnet/corefx
https://github.com/dotnet/corefx/tree/master/src/System.Json

から System.Json の src 以下をごっそり持ってきます。

Visual Studio 2015 でクラスライブラリを作成

実は、そのまま corefx のプロジェクトを持って来て少し変えるだけでいけるかなと思ったのですが、無理でした。

エラー時に System.SR という内部的なリソースにクラスにアクセスしているので、ビルドができません。仕方がないので、以下のようなダミークラスを作ります。

internal sealed class SR
{
    public static string ArgumentException_ArrayMustEndWithBracket { get; internal set; }
    public static object ArgumentException_ExpectedXButGotY { get; internal set; }
    public static object ArgumentException_ExpectedXDiferedAtY { get; internal set; }
    public static string ArgumentException_ExtraCharacters { get; internal set; }
    public static string ArgumentException_ExtraDot { get; internal set; }
    public static string ArgumentException_IncompleteEscapeLiteral { get; internal set; }
    public static string ArgumentException_IncompleteEscapeSequence { get; internal set; }
    public static string ArgumentException_IncompleteExponent { get; internal set; }
    public static string ArgumentException_IncompleteInput { get; internal set; }
    public static string ArgumentException_InvalidLiteralFormat { get; internal set; }
    public static string ArgumentException_LeadingZeros { get; internal set; }
    public static object ArgumentException_MessageAt { get; internal set; }
    public static string ArgumentException_NoDigitFound { get; internal set; }
    public static string ArgumentException_StringNotClosed { get; internal set; }
    public static object ArgumentException_UnexpectedCharacter { get; internal set; }
    public static string ArgumentException_UnexpectedEscapeCharacter { get; internal set; }
    public static object[] NotSupported_UnexpectedParserType { get; internal set; }

    internal static string Format( params object[]  para)
    {
        throw new NotImplementedException();
    }
}

これで、ビルドは通るようになりました。

呼び出し側は、

class Program
{
    static void Main(string[] args)
    {
        var json = @"{ ""name"": ""JSON"", ""number"": 13 }";
        var jr = new JavaScriptReader(new StringReader(json), false);
        var o = jr.Read();
        var pairs = o as KeyValuePair<string, object>[];

        Console.WriteLine("hello " + pairs[0].Value);
        Console.WriteLine("number is " + pairs[1].Value);
    }
}

な感じで、JavaScriptReader クラスを new して、Read メソッドを使います。元の System.Json のテストプロジェクトには JsonValue のテストコードしかないのですが、まあ、これでいけます。ただし、JavaScriptReader は internal なクラスになっているので、「public class JavaScriptReader」に書き替えてしまいます。

Read メソッドの戻り値は object 型なので、適当な型に直します。これ、最終的にどうするつもりかわからないのですが、ひとまず、KeyValuePairの配列で来ているので「KeyValuePair<string, object>[]」でキャストします。

実行すると、こんな感じで、アリスが JSON に出会えます。

どうも、プロパティ名はダブルクォートで囲まないと駄目なので、RFCに沿い過ぎているというか…「{ name: “JSON” }」な JavaScript の連想配列な JSON も通してほしいですね。そういうときは、NuGet で Newtonsoft.Json をインストールせよ、という話なんですが :)  コード的にはReadNumericLiteral でプロパティ名をクォートで拾っているので、それ修正すれば ok.

サンプルコード

AliceJson-v0.1-src.zip
https://1drv.ms/u/s!AmXmBbuizQkXgfxWteUSWlBU7zrT1g

カテゴリー: C# パーマリンク