maccman/sourcemap — pull request #9 by chancancode (2013-11-18 20:28:48+00:00)
Title: Improved JSON serialization

This should make `map.to_json` work as expected, and it's more or less compatible with ActiveSupport:

```
Projects/oss/sourcemap[master]% irb -Ilib
>> require 'json/pure'
=> true
>> require 'source_map/map'
=> true
>> include SourceMap
=> Object
>> hash = {
?> 'version' => 3,
?> 'file' => "script.min.js",
?> 'mappings' => "AAEAA,QAASA,MAAK,EAAG,CACfC,OAAAC,IAAA,CAAY,eAAZ,CADe",
?> 'sources' => ["script.js"],
?> 'names' => ["hello", "console", "log"]
>> }
=> {"version"=>3, "file"=>"script.min.js", "mappings"=>"AAEAA,QAASA,MAAK,EAAG,CACfC,OAAAC,IAAA,CAAY,eAAZ,CADe", "sources"=>["script.js"], "names"=>["hello", "console", "log"]}
>> map = Map.from_hash(hash)
=> #script.js@3:0#hello", "1:8->script.js@3:9#hello", "1:14->script.js@3:14", "1:16->script.js@3:17", "1:17->script.js@4:2#console", "1:24->script.js@4:2#log", "1:28->script.js@4:2", "1:29->script.js@4:14", "1:44->script.js@4:2", "1:45->script.js@3:17"]>
>> map.as_json
=> {"version"=>3, "file"=>"script.min.js", "mappings"=>"AAEAA,QAASA,MAAK,EAAG,CACfC,OAAAC,IAAA,CAAY,eAAZ,CADe", "sources"=>["script.js"], "names"=>["hello", "console", "log"]}
>> map.to_json
=> "{\"version\":3,\"file\":\"script.min.js\",\"mappings\":\"AAEAA,QAASA,MAAK,EAAG,CACfC,OAAAC,IAAA,CAAY,eAAZ,CADe\",\"sources\":[\"script.js\"],\"names\":[\"hello\",\"console\",\"log\"]}"
>> JSON.generate map
=> "{\"version\":3,\"file\":\"script.min.js\",\"mappings\":\"AAEAA,QAASA,MAAK,EAAG,CACfC,OAAAC,IAAA,CAAY,eAAZ,CADe\",\"sources\":[\"script.js\"],\"names\":[\"hello\",\"console\",\"log\"]}"
>> require 'active_support/all'
=> true
>> ActiveSupport::JSON.encode map
=> "{\"version\":3,\"file\":\"script.min.js\",\"mappings\":\"AAEAA,QAASA,MAAK,EAAG,CACfC,OAAAC,IAAA,CAAY,eAAZ,CADe\",\"sources\":[\"script.js\"],\"names\":[\"hello\",\"console\",\"log\"]}"
>> map.to_json
=> "{\"version\":3,\"file\":\"script.min.js\",\"mappings\":\"AAEAA,QAASA,MAAK,EAAG,CACfC,OAAAC,IAAA,CAAY,eAAZ,CADe\",\"sources\":[\"script.js\"],\"names\":[\"hello\",\"console\",\"log\"]}"
>> JSON.generate map
=> "{\"version\":3,\"file\":\"script.min.js\",\"mappings\":\"AAEAA,QAASA,MAAK,EAAG,CACfC,OAAAC,IAAA,CAAY,eAAZ,CADe\",\"sources\":[\"script.js\"],\"names\":[\"hello\",\"console\",\"log\"]}"
>> ActiveSupport::VERSION::STRING
=> "4.0.1"
>>
Projects/oss/sourcemap[master]% irb -Ilib
>> require 'json/ext'
=> true
>> require 'source_map/map'
=> true
>> include SourceMap
=> Object
>> hash = {
?> 'version' => 3,
?> 'file' => "script.min.js",
?> 'mappings' => "AAEAA,QAASA,MAAK,EAAG,CACfC,OAAAC,IAAA,CAAY,eAAZ,CADe",
?> 'sources' => ["script.js"],
?> 'names' => ["hello", "console", "log"]
>> }
=> {"version"=>3, "file"=>"script.min.js", "mappings"=>"AAEAA,QAASA,MAAK,EAAG,CACfC,OAAAC,IAAA,CAAY,eAAZ,CADe", "sources"=>["script.js"], "names"=>["hello", "console", "log"]}
>> map = Map.from_hash(hash)
=> #script.js@3:0#hello", "1:8->script.js@3:9#hello", "1:14->script.js@3:14", "1:16->script.js@3:17", "1:17->script.js@4:2#console", "1:24->script.js@4:2#log", "1:28->script.js@4:2", "1:29->script.js@4:14", "1:44->script.js@4:2", "1:45->script.js@3:17"]>
>> map.as_json
=> {"version"=>3, "file"=>"script.min.js", "mappings"=>"AAEAA,QAASA,MAAK,EAAG,CACfC,OAAAC,IAAA,CAAY,eAAZ,CADe", "sources"=>["script.js"], "names"=>["hello", "console", "log"]}
>> map.to_json
=> "{\"version\":3,\"file\":\"script.min.js\",\"mappings\":\"AAEAA,QAASA,MAAK,EAAG,CACfC,OAAAC,IAAA,CAAY,eAAZ,CADe\",\"sources\":[\"script.js\"],\"names\":[\"hello\",\"console\",\"log\"]}"
>> JSON.generate map
=> "{\"version\":3,\"file\":\"script.min.js\",\"mappings\":\"AAEAA,QAASA,MAAK,EAAG,CACfC,OAAAC,IAAA,CAAY,eAAZ,CADe\",\"sources\":[\"script.js\"],\"names\":[\"hello\",\"console\",\"log\"]}"
>> require 'active_support/all'
=> true
>> ActiveSupport::JSON.encode map
=> "{\"version\":3,\"file\":\"script.min.js\",\"mappings\":\"AAEAA,QAASA,MAAK,EAAG,CACfC,OAAAC,IAAA,CAAY,eAAZ,CADe\",\"sources\":[\"script.js\"],\"names\":[\"hello\",\"console\",\"log\"]}"
>> map.to_json
=> "{\"version\":3,\"file\":\"script.min.js\",\"mappings\":\"AAEAA,QAASA,MAAK,EAAG,CACfC,OAAAC,IAAA,CAAY,eAAZ,CADe\",\"sources\":[\"script.js\"],\"names\":[\"hello\",\"console\",\"log\"]}"
>> JSON.generate map
=> "{\"version\":3,\"file\":\"script.min.js\",\"mappings\":\"AAEAA,QAASA,MAAK,EAAG,CACfC,OAAAC,IAAA,CAAY,eAAZ,CADe\",\"sources\":[\"script.js\"],\"names\":[\"hello\",\"console\",\"log\"]}"
```