Parser101

So I was going to implement JSON or XML to make my game saves today, but I didn’t like them, so I made my own :smiley:

Here is basically an image that explains how it works.

Here are some code examples.


		ParserDevice device = new ParserDevice();
		
		device.put("apple", "a fruit.").put("color", "red");
		
		System.out.println(device.generateString());

prints


{"apple","a fruit.",{"color","red"}};

Here is 2nd example.


		ParserDevice device = new ParserDevice();

		// null put null as parent argument, if you don't want branch to be automatically added.
		ParserBranch branch = new ParserBranch("mercedes", "car", null);
		device.add(branch);

		branch.put("color", "white").put("color components", new float[] {1f, 1f, 1f});
		
		branch.put("engine", "powerful").put("horsepower", 1000);
		
		System.out.println(device.generateString());

prints

{"mercedes","car",{"color","white",{"color components","[1.0,1.0,1.0,]",{"type","float"},{"length","3"}}},{"engine","powerful",{"horsepower","1000"}}};

Here is a final example.


		ParserDevice device = new ParserDevice();

		device.put("mclaren", "formula 1 car").put("speed", "very fast");
		device.put("ferrari", "formula 1 car").put("color", "red");
		
		device.parseName("mclaren").put("gearbox", "7 gears");
		
		device.parseTree(new String[] {"ferrari", "color"}).put("color components", new int[] {255, 0, 0});
		
		System.out.println(device.generateString());

prints:

{"mclaren","formula 1 car",{"speed","very fast"},{"gearbox","7 gears"}}
{"ferrari","formula 1 car",{"color","red",{"color components","[255,0,0,]",{"type","integer"},{"length","3"}}}};

Here is the download link with src.
If you want to use/edit, you can do anything…
http://www.mediafire.com/download/6ece46z99aneuar/parser101.zip

Here is the download link with src.ere is the download link with src.
If you want to use/edit, you can do anything…f you want to use/edit, you can do anything…

To read the parser file, you need to create ParserDevice and give it a reader of the stream that contains parser format.

PARSER READER
Will cycle through branches that are within parsable object. If it runs out of branches to give you, it returns null. You can reset the reader with .reset

BUGS: ------------------------------------------------------------

  • You need to pass an array of strings of at least length 2 to .parseTree method. It will return null for some reason otherwise. Use .parseName for single string paths.

Cool stuff, the output of the parser looks a lot like JSON;
The mediafire link doesn’t seem to work, although I don’t have a mediafire account, so I’m not sure if that might be causing it;

With so many existing ways to handle tree based data what caused you to create your own?
I had some other questions that I think the code would help explain;

Anyway, interesting piece, thanks for posting;

Lol just started implementing this into my game and bugs all over already :smiley:
I will fix everything in the near future :smiley: This isn’t like “fully working” stuff.

When I looked at those json and xml parsers they looked really complicated to me. Besides, the one I created completely meets my needs, and I think it is simpler to use than most of xml and json parsers.

EDIT------------
I think you just happened to try and download the file while i was replacing it with another. :smiley:
Link should be working now.

Yep, the link is working now. The parser appears to be fixed to specific types though, what I mean is that you cannot read the next arbitrary chunk of data from the parser and determine the data’s type after reading it.
You have to know ahead of time what will come next in the file, else the parser will fail to read the correct data/array type. Which isn’t an error, just a design choice.

If you wanted, I could post some code that wraps one of the Java library’s XML parsers and removes the extraneous stuff, just leaving readTag() and readElement() methods.

Yea I’m starting to realize I need to add some .next functionality :stuck_out_tongue: I don’t think it would be too hard though.

Added parser reader so I can now “cycle” through objects pretty easily.