In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Dump.object

JSONから値を取り出すのがあまりにもめんどかったので、
* ついでにDumpするクラスを作ってみた。
* 
* Dump.object(data)だと、自動的にtraceされる。
* 
* Dump.stringFromObject(data)はStringが返る。
*
Get Adobe Flash player
by umhr 21 May 2010
    Embed
/**
 * Copyright umhr ( http://wonderfl.net/user/umhr )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/cfQD
 */

/*
 * JSONから値を取り出すのがあまりにもめんどかったので、
 * ついでにDumpするクラスを作ってみた。
 * 
 * Dump.object(data)だと、自動的にtraceされる。
 * 
 * Dump.stringFromObject(data)はStringが返る。
 * */

package {
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getQualifiedClassName;
    
    public class Main extends Sprite {
        public function Main() {
			
			var data:Object = { func:function():void { }, obj:{}, date:new Date(), txt:new TextField(), "null":null, array:new Array(), numbers:[101, 202, 303, "aaa"], director:"wolf", cast: { pig1:"Boo", pig2:"foo", pig3:"woo", house: { a:"strow", b:"woody", c:"bricky" } }};
			
			//traceされる
			Dump.object(data);
			
			var tf:TextField = new TextField();
			tf.defaultTextFormat = new TextFormat("_typewriter");
			tf.width = stage.stageWidth;
			tf.height = stage.stageHeight;
			tf.text = Dump.stringFromObject(data);
			this.addChild(tf);
			
        }
    }
}

import flash.utils.getQualifiedClassName;
class Dump {
	static public function stringFromObject(obj:Object):String {
		var str:String = _dump(obj);
		if (getQualifiedClassName(obj) == "Array") {
			str = "[\n" + str.slice( 0, -2 ) + "\n]";
		}else {
			str = "{\n" + str.slice( 0, -2 ) + "\n}";
		}
		return str;
	}
	static public function object(obj:Object):void {
		trace(stringFromObject(obj));
	}
	static private function _dump(obj:Object, indent:int = 0):String {
		var result:String = "";
		
		var da:String = (getQualifiedClassName(obj) == "Array")?'':'"';
		
		var tab:String = "";
		for ( var i:int = 0; i < indent; ++i ) {
			tab += "    ";
		}
		
		for (var key:String in obj) {
			if (typeof obj[key] == "object") {
				var type:String = getQualifiedClassName(obj[key]);
				if (type == "Object" || type == "Array") {
					result += tab + da + key + da + ":"+((type == "Array")?"[":"{");
					var dump_str:String = _dump(obj[key], indent + 1);
					if (dump_str.length > 0) {
						result += "\n" + dump_str.slice(0, -2) + "\n";
						result += tab;
					}
					result += (type == "Array")?"],\n":"},\n";
				}else {
					result += tab + '"' + key + '":<' + type + ">,\n";
				}
			}else if (typeof obj[key] == "function") {
				result += tab + '"' + key + '":<Function>,\n';
			}else {
				var dd:String = (typeof obj[key] == "string")?"'":"";
				result += tab + da + key + da + ":" + dd + obj[key] +dd + ",\n";
			}
		}
		return result;
	}
}