Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.Field;
- public class BadCodeSon {
- public static String toJson(Object object) throws Exception {
- if(object == null) {
- return "null";
- } if(object instanceof Number) {
- return String.valueOf(object);
- } else if(object instanceof String) {
- return "\"" + (String)object + "\"";
- } else if(object instanceof float[]) {
- float[] arr = (float[])object;
- String str = "[";
- for(int i = 0; i < arr.length; i++) {
- str += String.valueOf(arr[i]);
- if(i < arr.length - 1) {
- str += ",";
- }
- }
- return str + "]";
- } else if(object instanceof double[]) {
- double[] arr = (double[])object;
- String str = "[";
- for(int i = 0; i < arr.length; i++) {
- str += String.valueOf(arr[i]);
- if(i < arr.length - 1) {
- str += ",";
- }
- }
- return str + "]";
- } else if(object instanceof int[]) {
- int[] arr = (int[])object;
- String str = "[";
- for(int i = 0; i < arr.length; i++) {
- str += String.valueOf(arr[i]);
- if(i < arr.length - 1) {
- str += ",";
- }
- }
- return str + "]";
- } else if(object instanceof boolean[]) {
- boolean[] arr = (boolean[])object;
- String str = "[";
- for(int i = 0; i < arr.length; i++) {
- str += String.valueOf(arr[i]);
- if(i < arr.length - 1) {
- str += ",";
- }
- }
- return str + "]";
- } else if(object instanceof char[]) {
- char[] arr = (char[])object;
- String str = "[";
- for(int i = 0; i < arr.length; i++) {
- str += "\"" + String.valueOf(arr[i]) + "\"";
- if(i < arr.length - 1) {
- str += ",";
- }
- }
- return str + "]";
- } else if(object instanceof long[]) {
- long[] arr = (long[])object;
- String str = "[";
- for(int i = 0; i < arr.length; i++) {
- str += String.valueOf(arr[i]);
- if(i < arr.length - 1) {
- str += ",";
- }
- }
- return str + "]";
- } else if(object instanceof String[]) {
- String[] arr = (String[])object;
- String str = "[";
- for(int i = 0; i < arr.length; i++) {
- str += "\"" + String.valueOf(arr[i]) + "\"";
- if(i < arr.length - 1) {
- str += ",";
- }
- }
- return str + "]";
- } else if(object instanceof Boolean) {
- return object.toString();
- } else {
- Field[] declared_fields = object.getClass().getDeclaredFields();
- String str = "{";
- for(int i = 0; i < declared_fields.length; i++) {
- Field declared_field = declared_fields[i];
- if(!declared_field.isAccessible()) {
- declared_field.setAccessible(true); // don't give a shit about private fields
- }
- String name = declared_field.getName();
- Object v = declared_field.get(object);
- if(object.equals(v)) {
- str += "\"" + name + "\": " + "Circular";
- } else {
- str += "\"" + name + "\": " + toJson(v); // recursive methods yea boi
- }
- if(i < declared_fields.length - 1) {
- str += ", ";
- }
- }
- return str + "}";
- }
- }
- // test cases boi
- public static void main(String[] args) throws Exception {
- Test test = new Test();
- test.a = 50;
- test.b = "hello world!";
- test.c = 5.3f;
- test.d = new Test();
- test.d.a = 11;
- test.d.b = "yeet";
- test.d.c = (float)Math.sin(Math.PI);
- //test.d.d = test; // circulars ftw
- test.e = new double[] { 1.5d, 2.8d, Math.E };
- System.out.println(toJson(test));
- System.out.println(toJson(5f)); // not all json are objects
- System.out.println(toJson("hello world!"));
- System.out.println(toJson(new int[] { 5, 10, 15, 20 } ));
- System.out.println(toJson(new String[] { "a", "b", "c", "de" } ));
- System.out.println(toJson(null));
- }
- // test class for serialization
- static class Test {
- int a;
- String b;
- float c;
- Test d;
- double[] e;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment