com.juniform.JUniformPackerJSON.java Source code

Java tutorial

Introduction

Here is the source code for com.juniform.JUniformPackerJSON.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.juniform;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;

/**
 * Class that converts from JUniformObject to JSONValue and back
 * @author acherkashin
 */
public class JUniformPackerJSON implements IJUniformPacker {
    // ---------------------------------------------------------------------- //
    // PUBLIC
    // ---------------------------------------------------------------------- //

    @Override
    public JUniformObject toUniformObjectFromStream(InputStream stream) {
        try (InputStreamReader inputStreamReader = new InputStreamReader(stream)) {
            Object value = JSONValue.parseWithException(inputStreamReader);
            return this.toUniformObjectFromObject(value);
        } catch (IOException | ParseException ex) {
            return JUniformObject.OBJECT_NIL;
        }
    }

    @Override
    public JUniformObject toUniformObjectFromString(String string) {
        try {
            Object value = JSONValue.parseWithException(string);
            return this.toUniformObjectFromObject(value);
        } catch (ParseException ex) {
            return JUniformObject.OBJECT_NIL;
        }
    }

    @Override
    public JUniformObject toUniformObjectFromBytes(byte[] bytes) {
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        return this.toUniformObjectFromString(Charset.defaultCharset().decode(buffer).toString());
    }

    @Override
    public JUniformObject toUniformObjectFromObject(Object object) {
        if (object == null) {
            return JUniformObject.OBJECT_NIL;
        } else if (object instanceof JSONObject) {
            JSONObject jsonMap = (JSONObject) object;
            Map<Object, Object> map = new HashMap<>();
            Iterator it = jsonMap.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry) it.next();
                map.put(pair.getKey(), this.toUniformObjectFromObject(pair.getValue()));
            }
            return new JUniformObject(map);

        } else if (object instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) object;
            ArrayList<Object> array = new ArrayList<>();
            @SuppressWarnings("unchecked")
            Iterator<Object> it = jsonArray.iterator();
            while (it.hasNext()) {
                array.add(this.toUniformObjectFromObject(it.next()));
            }
            return new JUniformObject(array);
        } else {
            return new JUniformObject(object);
        }
    }

    @Override
    public Object fromUniformObject(JUniformObject object) {
        return this._toJSON(object);
    }

    @Override
    public String fromUniformObjectToString(JUniformObject object) {
        return JSONValue.toJSONString(this._toJSON(object));
    }

    @Override
    public byte[] fromUniformObjectToByteArray(JUniformObject object) {
        return this.fromUniformObjectToString(object).getBytes();
    }

    // ---------------------------------------------------------------------- //
    // PRIVATE
    // ---------------------------------------------------------------------- //

    @SuppressWarnings("unchecked")
    private Object _toJSON(JUniformObject object) {
        if (object == null) {
            return null;
        } else if (object.isMapValue()) {
            JSONObject jsonMap = new JSONObject();
            Iterator<Map.Entry<Object, JUniformObject>> it = object.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<Object, JUniformObject> pair = it.next();
                jsonMap.put(pair.getKey(), this._toJSON(pair.getValue()));
            }
            return jsonMap;

        } else if (object.isArrayValue()) {
            JSONArray jsonArray = new JSONArray();

            Iterator<JUniformObject> it = object.iterator();
            while (it.hasNext()) {
                jsonArray.add(this._toJSON(it.next()));
            }
            return jsonArray;
        } else {
            return object.getValue();
        }
    }
}