Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Parser101  (Read 1618 times)
0 Members and 1 Guest are viewing this topic.
Offline trollwarrior1
« Posted 2014-02-06 12:05:16 »

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 Cheesy

Here is basically an image that explains how it works.


Here are some code examples.

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

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






Here is 2nd example.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
      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
1  
{"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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
      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:
1  
2  
{"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.
Offline TeamworkGuy2

Junior Devvie


Medals: 10



« Reply #1 - Posted 2014-02-06 16:05:54 »

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;
Offline trollwarrior1
« Reply #2 - Posted 2014-02-06 16:10:05 »

Lol just started implementing this into my game and bugs all over already Cheesy
I will fix everything in the near future Cheesy 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. Cheesy
Link should be working now.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline TeamworkGuy2

Junior Devvie


Medals: 10



« Reply #3 - Posted 2014-02-06 16:44:35 »

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.
Offline trollwarrior1
« Reply #4 - Posted 2014-02-06 17:00:51 »

Yea I'm starting to realize I need to add some .next functionality Tongue I don't think it would be too hard though.
Offline trollwarrior1
« Reply #5 - Posted 2014-02-10 09:24:05 »

Added parser reader so I can now "cycle" through objects pretty easily.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (57 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!