Advertisement
Guest User

JSON klass

a guest
Jul 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. package com.exoticdev.fantasyworldgenerator.configuration.json;
  2.  
  3.  
  4. import org.json.simple.JSONObject;
  5. import org.json.simple.parser.JSONParser;
  6.  
  7. import java.io.File;
  8. import java.io.FileReader;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. public class JSONReader {
  13.  
  14. private Object parsed;
  15.  
  16. public JSONReader(File file) {
  17. try {
  18. JSONParser parser = new JSONParser();
  19.  
  20. this.parsed = parser.parse(new FileReader(file.getPath()));
  21. } catch (Exception exception) {
  22. exception.printStackTrace();
  23. }
  24. }
  25.  
  26. private JSONObject getJSONObject(String object) {
  27. JSONObject jsonObject = (JSONObject) parsed;
  28.  
  29. if(!(object.contains("."))) {
  30. return (JSONObject) jsonObject.get(object);
  31. } else {
  32. List<String> objects = new ArrayList<>();
  33.  
  34. StringBuilder sb = new StringBuilder();
  35.  
  36. for(int i = 0; i < object.length(); i++) {
  37. String character = Character.toString(object.charAt(i));
  38.  
  39. if(character.equalsIgnoreCase(".")) {
  40. if(!(sb.toString().equalsIgnoreCase(""))) {
  41. objects.add(sb.toString());
  42. }
  43.  
  44. sb.setLength(0);
  45. } else {
  46. sb.append(character);
  47. }
  48. }
  49.  
  50. if(!(sb.toString().equalsIgnoreCase(""))) {
  51. objects.add(sb.toString());
  52. }
  53.  
  54. for(String objectName : objects) {
  55. jsonObject = (JSONObject) jsonObject.get(objectName);
  56. }
  57.  
  58. return jsonObject;
  59. }
  60. }
  61.  
  62. private String getObject(String object) {
  63. if(object.contains(".")) {
  64. int last_dot = 0;
  65.  
  66. for(int i = 0; i < object.length(); i++) {
  67. String character = Character.toString(object.charAt(i));
  68.  
  69. if(character.equalsIgnoreCase(".")) {
  70. last_dot = i;
  71. }
  72. }
  73.  
  74. String key;
  75. JSONObject jsonObject;
  76.  
  77. StringBuilder key_builder = new StringBuilder();
  78. StringBuilder object_builder = new StringBuilder();
  79.  
  80. for(int i = last_dot + 1; i < object.length(); i++) {
  81. key_builder.append(Character.toString(object.charAt(i)));
  82. }
  83.  
  84. for(int i = 0; i < last_dot; i++) {
  85. object_builder.append(Character.toString(object.charAt(i)));
  86. }
  87.  
  88. key = key_builder.toString();
  89.  
  90. jsonObject = this.getJSONObject(object_builder.toString());
  91.  
  92. Object finalObject = jsonObject.get(key);
  93.  
  94. return finalObject.toString();
  95. }
  96.  
  97. return null;
  98. }
  99.  
  100. public int getInt(String object) {
  101. return Integer.parseInt(this.getObject(object));
  102. }
  103.  
  104. public double getDouble(String object) {
  105. return Double.parseDouble(this.getObject(object));
  106. }
  107.  
  108. public boolean getBoolean(String object) {
  109. System.out.println("Boolean: " + object + " resulted in: " + Boolean.parseBoolean(this.getObject(object)));
  110. return Boolean.parseBoolean(this.getObject(object));
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement