All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jupyterkernel.json.messages.T_JSON Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
/*
 * 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 org.jupyterkernel.json.messages;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jupyterkernel.util.JSONField;

/**
 *
 * @author kay schluehr
 */
public class T_JSON {
    public static String message_protocol_version = null;
    
    private static double protocol_version = 0.0;
    
    public static void setProtocolVersion(String protocolVersion)
    {
        message_protocol_version = protocolVersion;
        protocol_version = Double.parseDouble(protocolVersion);
        
    }
    

    public static T_JSON fromJSON(String classname, JSONObject jsonObj) {

        T_JSON instance;
        Object value;
        try {
            Class cls = Class.forName("org.jupyterkernel.json.messages." + classname);
            try {
                instance = (T_JSON) cls.newInstance();
                if (jsonObj.length() == 0) {
                    return instance;
                }
            } catch (InstantiationException | IllegalAccessException ex) {
                Logger.getLogger(T_JSON.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(T_JSON.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
        Field[] fields = instance.getClass().getFields();
        for (Field field : fields) {
            JSONField jsonField = field.getAnnotation(JSONField.class);
            if (jsonField != null) {
                String name = field.getName();                
                try {
                    value = jsonObj.get(name);
                } catch (JSONException e) {
                    continue;
                }
                if (jsonField.type().equals("T_JSON")) {
                    if (name.equals("content")) {
                        name = (String) jsonObj.getJSONObject("header").get("msg_type");
                    }
                    value = fromJSON("T_" + name, (JSONObject) value);
                }
                try {
                    field.set(instance, value);
                } catch (IllegalArgumentException | IllegalAccessException ex) {
                    Logger.getLogger(T_JSON.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return instance;
    }
    
    private boolean checkVersion(String versionString)
    {
        int n = versionString.length()-1;
        char qual = versionString.charAt(n);
        double version = Double.parseDouble(versionString.substring(0, n));
        switch(qual)
        {
            case '+':
            {
                return (protocol_version>=version);
            }
            case '-':
            {
                return (protocol_version




© 2015 - 2024 Weber Informatics LLC | Privacy Policy