JSON ENCODER/DECODER
RPG Maker VX Ace
Easily decode JSON into Ruby objects and vica versa.
- GameGuysProjects
- 02/28/2012 11:36 PM
- 7944 views
JSON Encoder/Decoder
Authors: game_guy
Version: 1.1
Type: Script Utility
Authors: game_guy
Version: 1.1
Type: Script Utility
Introduction
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.
This is a simple JSON Parser or Decoder. It'll take JSON thats been formatted into a string and decode it into the proper object. You can also encode certain Ruby objects into JSON.
This is a scripter's utility and isn't meant to add any new functionality or prove usefulness to non-scripters.
Features
- Decodes JSON format into ruby strings, arrays, hashes, integers, booleans.
- Encodes Ruby objects into JSON format.
Screenshots
N/A
Demo
No demo.
Instructions
Its recommended to place this script at the top, then it can be used in any script below it. To call the parsing method, use
JSON Decode Example
JSON.decode(json) Returns any of the following depending on "json": -Integer -String -Boolean -Hash -Array -Nil
Example:
json = '{"result":true,"profile":{"id":23,"display_name":"game_guy","group":{"id":"1","name":"Administrator","color":"#FF0000"}},"array":[0,1,2,3]}' data = JSON.decode(json)
Output
data = {'result' => true, 'profile' => {'id' => 23, 'display_name' => 'game_guy', 'group' => {'id' => 1, 'name' => 'Administrator', 'color' => '#FF0000'}}, 'array' => [0, 1, 2, 3]}
As you can see, it supports multi-level hashes and the method returned a hash object since the string started with a "{". The first character is what defines what object its going to return.
JSON Encode Example
This can encode the following Ruby objects into JSON format.
Integer Boolean String Hash Array Nil
Here's your input.
data = {'result' => true, 'profile' => {'id' => 23, 'display_name' => 'game_guy', 'group' => {'id' => 1, 'name' => 'Administrator', 'color' => '#FF0000'}}, 'array' => [0, 1, 2, 3]} json = JSON.encode(data)
Output
'{"result":true,"profile":{"id":23,"display_name":"game_guy","group":{"id":"1","name":"Administrator","color":"#FF0000"}},"array":[0,1,2,3]}'
Compatibility
Should work with anything.
Credits and Thanks
- game_guy ~ For creating it.
Author's Notes
Enjoy! :) You guys are lucky that VX Ace uses Ruby 1.9, or else this script wouldn't have been released. With the example code I posted, it takes a few seconds for Ruby 1.8 to parse it all.
Actions
Posts
Pages:
1
Hmm...I've been taught JSON before, but never got around it as to when to use it. What ideal situation would this be better to use than simply writing your own formatted string algorithms for reading data between compilers.
author=korodo
Hmm...I've been taught JSON before, but never got around it as to when to use it. What ideal situation would this be better to use than simply writing your own formatted string algorithms for reading data between compilers.
I actually wrote this for a friend's game. I have a special DLL I created in C and it gets called within the game. What this does is grabs user data from the forum in JSON format and this script will parse it. I can't think of any other actual situations as to why this would be useful, but thats why its a scripters tool, thats for other people who might need this to decide.
author=korodo
Hmm...I've been taught JSON before, but never got around it as to when to use it. What ideal situation would this be better to use than simply writing your own formatted string algorithms for reading data between compilers.
You use JSON because it is a standard, and well, standards are standard. This means that when someone else has to use or extend your code, they can say "hey look, this will take a JSON formatted string" or "hey look, this outputs a JSON formatted string" and they know how to interact with it without having to learn your obscure custom methods.
This is not a proper JSON parser. The JSON spec allows for whitespace and end of lines and this parser will fail on both of those. To fix this so you don't need to strip them out before calling this add the following line:
To the next_token method after the @index += 1 so it looks like this:
With this it can parse JSON strings that have whitespace and EOLs in them without issue.
return next_token if char.ord <= 32 and char.ord != 0
To the next_token method after the @index += 1 so it looks like this:
def self.next_token(debug = 0) char = @json[@index, 1] @index += 1 # Don't try to tokenize this character if it isn't a character # (captures EOL (10) and whitespace (32)) return next_token if char.ord <= 32 and char.ord != 0 case char when '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-' return TOKEN_NUMBER ...
With this it can parse JSON strings that have whitespace and EOLs in them without issue.
I've uploaded this script to GitHub, including the patch from GreatReadSpirit.
https://github.com/ashes999/rpg-maker-resources/blob/gh-pages/vx-ace/scripts/json_parser.rb
https://github.com/ashes999/rpg-maker-resources/blob/gh-pages/vx-ace/scripts/json_parser.rb
Pages:
1