Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: trollwarrior1 (posted 2014-07-02 18:22:41, viewed 36 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299  
package com.guit.core;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.parser.ParseException;

public class GJsonObject {

   private JSONObject json;
   private List<GJsonObject> children = new ArrayList<GJsonObject>();
   private String name = null;
   private GJsonObject parent = null;

   public GJsonObject() {
      json = new JSONObject();
   }

   public GJsonObject put(String name, Object obj) {
      json.put(name, obj);
      GJsonObject newJson = new GJsonObject();
      newJson.parent = this;
      newJson.name = name;
      children.add(newJson);
      return this;
   }

   public GJsonObject put(String name) {
      GJsonObject newJson = new GJsonObject();
      newJson.name = name;
      newJson.parent = this;
      children.add(newJson);
      json.put(name, newJson.json);
      return newJson;
   }

   public Object getData() {
      Object data = parent.json.get(name);
      return data;
   }

   @SuppressWarnings("unchecked")
   private <T> T getData(Class<T> cls) {
      Object obj = getData();
      try {
         return (T) obj;
      } catch (Exception e) {
         throw new IllegalArgumentException(String.format("Cannot cast FROM:%s TO:%s", obj.getClass().getSimpleName(), cls.getSimpleName()));
      }
   }

   public long getLong() {
      return (Long.parseLong(getData().toString()));
   }

   public int getInt() {
      return (Integer.parseInt(getData().toString()));
   }

   public float getFloat() {
      return (Float.parseFloat(getData().toString()));
   }

   public double getDouble() {
      return (Double.parseDouble(getData().toString()));
   }

   public short getShort() {
      return (Short.parseShort(getData().toString()));
   }

   public byte getByte() {
      return (Byte.parseByte(getData().toString()));
   }

   public char getChar() {
      return getData(char.class);
   }

   public boolean getBoolean() {
      return getData(boolean.class);
   }

   public String getString() {
      return getData(String.class);
   }

   public GJsonObject findNode(String name) {
      return findNode(name, false);
   }

   public long[] toLongArray() {
      JSONArray jsonArray = (JSONArray) getData();

      long[] array = new long[jsonArray.size()];

      for (int i = 0; i < jsonArray.size(); i++) {
         array[i] = (Long) jsonArray.get(i);
      }

      return array;
   }

   public int[] toIntArray() {
      JSONArray jsonArray = (JSONArray) getData();

      int[] array = new int[jsonArray.size()];

      for (int i = 0; i < jsonArray.size(); i++) {
         array[i] = (Integer) jsonArray.get(i);
      }

      return array;
   }

   public float[] toFloatArray() {
      JSONArray jsonArray = (JSONArray) getData();

      float[] array = new float[jsonArray.size()];

      for (int i = 0; i < jsonArray.size(); i++) {
         array[i] = (Float) jsonArray.get(i);
      }

      return array;
   }

   public double[] toDoubleArray() {
      JSONArray jsonArray = (JSONArray) getData();

      double[] array = new double[jsonArray.size()];

      for (int i = 0; i < jsonArray.size(); i++) {
         array[i] = (Double) jsonArray.get(i);
      }

      return array;
   }

   public short[] toShortArray() {
      JSONArray jsonArray = (JSONArray) getData();

      short[] array = new short[jsonArray.size()];

      for (int i = 0; i < jsonArray.size(); i++) {
         array[i] = (Short) jsonArray.get(i);
      }

      return array;
   }

   public byte[] toByteArray() {
      JSONArray jsonArray = (JSONArray) getData();

      byte[] array = new byte[jsonArray.size()];

      for (int i = 0; i < jsonArray.size(); i++) {
         array[i] = (Byte) jsonArray.get(i);
      }

      return array;
   }

   public char[] toCharArray() {
      JSONArray jsonArray = (JSONArray) getData();

      char[] array = new char[jsonArray.size()];

      for (int i = 0; i < jsonArray.size(); i++) {
         array[i] = (Character) jsonArray.get(i);
      }

      return array;
   }

   public boolean[] toBooleanArray() {
      JSONArray jsonArray = (JSONArray) getData();

      boolean[] array = new boolean[jsonArray.size()];

      for (int i = 0; i < jsonArray.size(); i++) {
         array[i] = (Boolean) jsonArray.get(i);
      }

      return array;
   }

   public String[] toStringArray() {
      JSONArray jsonArray = (JSONArray) getData();

      String[] array = new String[jsonArray.size()];

      for (int i = 0; i < jsonArray.size(); i++) {
         array[i] = (String) jsonArray.get(i);
      }

      return array;
   }

   public GJsonObject findNode(String name, boolean recursive) {

      if (recursive) {
         if (this.name != null && this.name.equals(name)) return this;

         for (GJsonObject child : children) {
            GJsonObject returnJson = child.findNode(name, true);
            if (returnJson != null) return returnJson;
         }

         return null;
      }

      for (GJsonObject child : children) {
         if (child.name.equals(name)) return child;
      }
      return null;
   }

   public String getName() {
      return name;
   }

   public JSONObject getJSONObject() {
      return json;
   }

   public String toString() {
      return json.toJSONString();
   }

   public List<GJsonObject> getChildren() {
      return children;
   }

   public GJsonObject getParent() {
      return parent;
   }

   private void parse(JSONObject JSON) {
      Set<String> set = JSON.keySet();
      Iterator<String> it = set.iterator();
      while (it.hasNext()) {
         String str = it.next();
         GJsonObject json = new GJsonObject();
         json.parent = this;
         json.name = str;
         children.add(json);

         Object child = JSON.get(str);
         if (child instanceof JSONObject) {
            JSONObject JSONChild = (JSONObject) child;
            json.json = JSONChild;
            json.parse(JSONChild);
         }
      }

   }

   public static GJsonObject parse(InputStream stream) {
      JSONParser parser = new JSONParser(JSONParser.MODE_RFC4627);

      try {

         GJsonObject json = new GJsonObject();
         JSONObject JSON = (JSONObject) parser.parse(stream);

         json.parse(JSON);

         return json;
      } catch (ParseException e) {
         e.printStackTrace();
      }

      return null;
   }

   public static GJsonObject parse(JSONParser parser, String jsonString) {

      try {
         GJsonObject json = new GJsonObject();
         JSONObject JSON = (JSONObject) parser.parse(jsonString);

         json.parse(JSON);

         return json;
      } catch (ParseException e) {
         e.printStackTrace();
      }

      return null;
   }

}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

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

The first screenshot will be displayed as a thumbnail.

atombrot (27 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!