Skip to content
This repository has been archived by the owner on Jan 15, 2020. It is now read-only.
/ Mojangson Public archive

A simplistic parsing and writing library for Mojangson, a variant of JSON used for NBT structure representation.

License

Notifications You must be signed in to change notification settings

aramperes/Mojangson

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What is Mojangson?

Mojangson is a variant of JSON, used in the voxel-based game Minecraft by Mojang. It's current use is to get a readable representation of Data tags, which are used to read and modify data from technical entities of the game.

Mojangson is slightly different than JSON because of it's relative flexibility. For example, certain rules such as double-quotes for element key names are not mandatory. However, the data representation can be somewhat variable, numerical values may need an extra prefix with their literal value, which makes the use of standart JSON parsers impossible.

Mojangson can represent different literal values: Byte, Double, Float, Integer, Long, Short and String. The syntax also supports the use of Compounds, which is a nested Map with a standart key-value pair syntax, and the use of Arrays, which is a List of values (without a key) of the same type.

API Example - Compound

This is an example of a simple Mojangson compound string:

{Name:"Momo", Age:100s, Height:1.89d}

This compound can be broken down like this:

{ // Compound start
    Name:"Momo", // MojangsonString value, key is 'Name'
    Age:100s,     // MojangsonShort value ('s' suffix), key is 'Age'
    Height:1.89d // MojangsonDouble value ('d' suffix), key is 'Height'
} // Compound end

In order to represent the compound, we need to invoke the MojangsonCompound#read(String) method:

String mojangson = "{Name:\"Momo\", Age:100s, Height:1.89d}";
MojangsonCompound compound = new MojangsonCompound();
try {
    compound.read(string);
} catch (MojangsonParseException e) {
    e.printStackTrace();
}

If we want to add an element to the Compound, we can use the MojangsonCompound#put(key,value) method:

MojangsonInt mInt = new MojangsonInt(9001);
compound.put("Level", mInt);

Finally, if we want to represent the Compound back into a Mojangson string:

StringBuilder sb = new StringBuilder();
compound.write(sb);
String mojangson = sb.toString();

API Example - Arrays

Arrays are a list of elements of the same type. They do not have a key-value pair element list, but rather all the values put together.

To create an Array, in this case an Array of Strings, you can call the constructor:

MojangsonArray<MojangsonString> array = new MojangsonArray<>(MojangsonString.class);

To add elements to the Array, you can use the List#add(MojangsonString) method:

array.add(new MojangsonString("Hello"));
array.add(new MojangsonString("World"));

Finally, to print the array:

StringBuilder builder = new StringBuilder();
array.write(builder);
System.out.println(builder.toString()); // Prints ["Hello","World"]

About

A simplistic parsing and writing library for Mojangson, a variant of JSON used for NBT structure representation.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages