こんにちは. taro (@9re)です.

暑い日が続きますね.

今日はas3corelibのJSONライブラリについてちょっとみてみます.

最近の動きは

  • as3corelib 0.93リリース(5/28)
  • as3corelib githubへ移行(7/29)

という感じ。

PV3D 3.0Stats等のライブラリがgithubで公開される中、as3corelibも遂に移行してしまったようです.

googlecodeの方には、

 

as3corelib_googlecode.png

 

とも出ています.

githubの方がパッチを受け入れやすいというのが移行に踏み切った大きな要因だそうです.

 

0.93での大きな変更点

JSON.decodeでJSON standardに準拠しないようなJSONもパース出来るようになりました.

JSONDecoderを直接newしているようなアプリケーションではコンストラクタの引数が1から2に変ったので互換性の問題が起きます.

JSONDecoder() Constructor
public function JSONDecoder(s:String, strict:Boolean)
Constructs a new JSONDecoder to parse a JSON string into a native object.

Parameters
s:String -- The JSON string to be converted into a native object
strict:Boolean -- Flag indicating if the JSON string needs to strictly match the JSON standard or not.

では、どのようなjsonがパース出来るようになったのでしょうか. 3だけ注意が必要です(*)

  1. , で終わる配列を許容する
  2. , で終わる連想配列を許容する
  3. エスケープされていない文字(0x00-0x1F)の許容 (*)
  4. 16進数のパース
  5. NaNも正しいtoken
  6. nbsp(non breaking space = String.fromCharCode(160))も空白文字として認められる
  7. 終端の不要文字の許容

と言われましても・・・ということで、

 

早速試してみる

それぞれの項目がチェック出来そうな式を並べて書いていきます。

// , で終わる配列を許容する
test('[1,2,3,]');
// , で終わる連想配列を許容する
test('{"key":"value",}');
// エスケープされていない文字(0x00-0x1F)の許容
test('"hi,\nthere!"');
// 16進数のパース 表示は16進数で
test('0xffffff', function(o:int):String { return o.toString(16); } );
// NaNも正しいtoken 表示はtoString()する
test('NaN', function(o:Number):String { return o + ''; } );
// nbsp(non breaking space = String.fromCharCode(160))も空白文字として認められる
test(String.fromCharCode(160) + '"nbsp"');
// 終端の不要文字の許容
test('"end"xxx');

function test(json:String, printer:Function = null):void {
    // 表示用の関数がnullならJSON.encodeで表示する
    printer ||= JSON.encode;
    // 元のJSONとstrit mode = falseでdecodeした結果を表示する
    tf.appendText(json + ' => ' + printer(JSON.decode(json, false)) + '\n');
}

これではまだ動かないコードですので、必要な変数の値等を定義します.

Flash IDEをお使いの方は、フレーム・スクリプトにJSONTest()の中身だけを貼りつけると良いでしょう.

import文はどこに書いても良いので、不自然ですがコンストラクタ内に置きました.

package {
  import flash.display.Sprite;
  import flash.text.TextField;
  public class JSONTest extends Sprite {
    public function JSONTest() {
      import com.adobe.serialization.json.JSON;
      // 表示用TextField
      var tf:TextField = new TextField;
      addChild(tf);
      // 表示用関数
      // , で終わる配列を許容する?
      test('[1,2,3,]');
      // , で終わる連想配列を許容する
      test('{"key":"value",}');
      // エスケープされていない文字(0x00-0x1F)の許容
      test('"hi,\nthere!\r\tunescaped characters\"');
      // 16進数のパース 表示は16進数で
      test('0xffffff', function(o:int):String { return o.toString(16); } );
      // NaNも正しいtoken 表示はtoString()する
      test('NaN', function(o:Number):String { return o + ''; } );
      // nbsp(non breaking space = String.fromCharCode(160))も空白文字として認められる
      test(String.fromCharCode(160) + '"nbsp"');
      // 終端の不要文字の許容
      test('"end"xxx');
      // リサイズ
      tf.width = tf.textWidth + 4;
      tf.height = tf.textHeight + 4;

      function test(json:String, printer:Function = null):void {
        // 表示用の関数がnullならJSON.encodeで表示する
        printer ||= JSON.encode;
        // 元のJSONとstrit mode = falseでdecodeした結果を表示する
        tf.appendText(json + ' => ' + printer(JSON.decode(json, false)) + '\n');
      }
    }
  }
}

どのJSONも上手くパースされているようです.

 

 

逆にstrictモードでパース出来なくなったJSON

ここではas3corelib 0.92ではパース出来たのに、strictモード(デフォルト)でパース出来なくなったJSONを見てみましょう.

  • エスケープされていない文字(0x00-0x1F)の許容

を見たとき、お気づきになったかもしれませんが、0.92では出来ましたよね?

はい。ではwonderflで検証です!

unescaped characters in json - wonderfl build flash online

 

勿論このような改行などのエスケープされるべき文字がエスケープされていないJSONは0.93のstrictモードでエラーを起こします.

 

エスケープ文字に関するエラーメッセージが表示されます.

では、また!!

HTML5飯