Java - 为什么我的JSON方法中出现stackoverflow错误?

时间:2016-02-26 14:06:29

标签: java json

我需要你的帮助才能理解为什么在我的方法中对象分配会返回Stack Overflow错误。

以下是方法:

public String toJson(){

        JSONObject json;
        json = new JSONObject(this); //error happens here...

        return json.toString();
    }

此类中的所有代码:

package com.neocloud.model;

import java.io.Serializable;

import com.amazonaws.util.json.JSONObject;
import com.neocloud.amazon.AwsSns;
import com.neocloud.model.dao.DeviceDAO;
import com.neocloud.model.dao.GroupDAO;
import com.neocloud.model.dao.SubscriptionDAO;

public class Subscription implements Serializable{

    private static final long serialVersionUID = -3901714994276495605L;
    private long id;
    private Device dispositivo;
    private Group  grupo;
    private String subscription;


    public Subscription(){

        clear();

    }

    public void clear(){

        id = 0;
        subscription = "";
        grupo = new Group();
        dispositivo = new Device();

    }

    public String toJson(){

        JSONObject json;
        json = new JSONObject(this); //error happens here...

        return json.toString();
    }


    public String registerDB(boolean registrarSNS){

        GroupDAO gDAO = new GroupDAO();
        DeviceDAO dDAO = new DeviceDAO();

        gDAO.selectById(this.grupo);
        dDAO.selectById(this.dispositivo);

        if (registrarSNS)
            registerSNS(this.dispositivo.getEndPointArn(), this.grupo.getTopicArn());

        SubscriptionDAO sDAO = new SubscriptionDAO();
        return sDAO.insert(this);
    }

    public String registerSNS(String endpointArn, String topicArn){

        String resposta;
        AwsSns sns = AwsSns.getInstance();
        resposta = ""+sns.addSubscriptionToTopic(endpointArn, topicArn);
        setSubscription(resposta);

        return resposta;        
    }

    public String delete(){

        AwsSns sns = AwsSns.getInstance();
        SubscriptionDAO sDAO = new SubscriptionDAO();
        sDAO.selectById(this);

        sns.deleteSubscriptionFromTopic(this.subscription);

        return sDAO.delete(this);

    }


    public long getId() {
        return id;
    }

    public void setId(long id) {
        if (this.id != id)
            this.id = id;
    }

    public String getSubscription() {
        return subscription;
    }

    public void setSubscription(String subscription) {
        if (subscription != null){
            if (!this.subscription.equals(subscription))
                this.subscription = subscription;
        }else
            this.subscription = new String();
    }

    public Device getDispositivo() {
        return dispositivo;
    }

    public void setDispositivo(Device dispositivo) {
        if (dispositivo != null){
            if (dispositivo.getId() != this.dispositivo.getId())
                this.dispositivo = dispositivo;
        }else
            this.dispositivo = new Device();
    }

    public Group getGrupo() {
        return grupo;
    }

    public void setGrupo(Group grupo) {
        if (grupo != null){
            if (grupo.getId() != this.grupo.getId())
                this.grupo = grupo;
        }else
            this.grupo = new Group();
    }

}

这是Device类:

package com.neocloud.model;

import java.io.Serializable;
import java.util.ArrayList;

import com.amazonaws.util.json.JSONException;
import com.amazonaws.util.json.JSONObject;
import com.neocloud.amazon.AwsSns;
import com.neocloud.model.dao.DeviceDAO;
import com.neocloud.model.enums.EEnvironment;
import com.neocloud.model.enums.EProduct;
import com.neocloud.model.enums.ESystem;

public class Device implements Serializable{


    private static final long serialVersionUID = 8515474788917476721L;
    private long id;
    private String development;
    private String SSL = "tls://gateway.push.apple.com:2195";
    private String feedback = "tls://feedback.push.apple.com:2196";
    private String sandboxSSL = "tls://gateway.sandbox.push.apple.com:2195";
    private String sandboxFeedback = "tls://feedback.sandbox.push.apple.com:2196";
    private String message;
    private String endPointArn;
    private String deviceToken;
    private EProduct appID;
    private String appVersion;
    private String deviceUID;
    private String deviceName;
    private String deviceModel;
    private String deviceVersion;
    private boolean pushBadge;
    private boolean pushAlert;
    private boolean pushSound;
    private ESystem system;
    private EEnvironment environment;
    private ArrayList<Subscription> subscriptions;
    private String deviceUser;


    public Device(){

        clear();

    }

    public String deleteToken(){
        /*
         * 
         * terminar aqui ainda...
         * 
        DeviceDAO dDAO = new DeviceDAO();

        AwsSns sns = AwsSns.getInstance();

        sns.deleteDeviceEndpoint(getEndPointArn());

        ArrayList<String> subscriptions = dDAO.getSubscriptionsFromEndpointArn(this);

        for (int i=0; i<subscriptions.size();i++){

            sns.deleteSubscriptionFromTopic(subscriptions.get(i));

        }

        return dDAO.delete(this);
        */ return "";
    }

    public String updateToken(){

        if ((system == ESystem.IOS) && (deviceToken.length() != 64))
            return "deviceTokenlen64";

        AwsSns sns = AwsSns.getInstance();
        String jsonResposta = ""+sns.updateDeviceEndpoint(getEndPointArn(), deviceToken);

        DeviceDAO dDAO = new DeviceDAO();
        dDAO.update(this);

        return jsonResposta;

    }

    public String registerDevice(){

        if (appVersion.length() == 0)
            return "applen0";
        if (deviceUID.length() > 40)
            return "deviceUID40";
        if ((system == ESystem.IOS) && (deviceToken.length() != 64))
            return "deviceTokenlen64";
        if (deviceName.length() == 0)
            return "deviceNamelen0";
        if (deviceModel.length() == 0)
            return "deviceModellen0";
        if (deviceVersion.length() == 0)
            return "deviceVersionlen0";

        AwsSns sns = AwsSns.getInstance();
        String jsonResposta = null;

        switch (getAppID()) {
            case MODULE: {
                if (system == ESystem.IOS)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTMODULEIOS);
                else if (system == ESystem.ANDROID)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTMODULEANDROID);
                break;

            }
            case HOSTPRO: {
                if (system == ESystem.IOS)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTHOSTPROIOS);
                else if (system == ESystem.ANDROID)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTHOSTPROANDROID);                  
                break;

            }
            case CONNEXOON: {
                if (system == ESystem.IOS)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTCONNEXOONIOS);
                else if (system == ESystem.ANDROID)
                    jsonResposta = ""+sns.createDeviceEndpoint(deviceToken, Constants.ENDPOINTCONNEXOONANDROID);
                break;

            }
            case TAHOMA: {

                break;
            }
            case MINIBOX: {

                break;
            }
            case NONE: {

            }
            default: {

            }
        }


        if (jsonResposta != null){

            JSONObject json;
            try {
                jsonResposta = jsonResposta.replace("}", "\"}").replace("arn", "\"arn");
                jsonResposta = jsonResposta.replace("{EndPointArn:", "{\"EndPointArn\":");
                json = new JSONObject(jsonResposta);
                setEndPointArn(json.getString("EndpointArn"));
            } catch (JSONException e) {

                e.printStackTrace();
            }

        }

        Subscription subscription;

        switch (getAppID()){
            case MODULE: {

                subscription = new Subscription();
                subscription.getGrupo().setId(19);
                subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICMODULEGERAL);
                subscriptions.add(subscription);


                if (system == ESystem.IOS){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(25);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICMODULEIOS);
                    subscriptions.add(subscription);

                }
                else if (system == ESystem.ANDROID){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(22);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICMODULEANDROID);
                    subscriptions.add(subscription);

                }
                break;

            }
            case HOSTPRO: {

                subscription = new Subscription();
                subscription.getGrupo().setId(10);
                subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICHOSTPROGERAL);
                subscriptions.add(subscription);

                if (system == ESystem.IOS){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(16);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICHOSTPROIOS);
                    subscriptions.add(subscription);

                }
                else if (system == ESystem.ANDROID){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(13);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICHOSTPROANDROID);
                    subscriptions.add(subscription);

                }   
                break;

            }
            case CONNEXOON: {

                subscription = new Subscription();
                subscription.getGrupo().setId(1);
                subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICCONNEXOONGERAL);
                subscriptions.add(subscription);

                if (system == ESystem.IOS){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(7);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICCONNEXOONIOS);
                    subscriptions.add(subscription);

                }
                else if (system == ESystem.ANDROID){

                    subscription = new Subscription();
                    subscription.getGrupo().setId(4);
                    subscription.registerSNS(getEndPointArn(), Constants.ENDPOINTTOPICCONNEXOONANDROID);
                    subscriptions.add(subscription);

                }
                break;

            }
            case TAHOMA: {

                break;
            }
            case MINIBOX: {

                break;
            }
            case NONE: {

            }
            default: {

            }
        }

        DeviceDAO dDAO = new DeviceDAO();
        return dDAO.insert(this);

    }


    public void clear(){

        development = "";
        message = "";
        endPointArn = "";
        deviceToken = "";
        appID = EProduct.NONE;
        appVersion = "";
        deviceUID = "";
        deviceName = "";
        deviceModel = "";
        deviceVersion = "";
        pushBadge = false;
        pushAlert = false;
        pushSound = false;
        system = ESystem.NONE;
        environment = EEnvironment.HOMOLOGATION;
        subscriptions = new ArrayList<Subscription>();
        deviceUser = "";

    }

    public String toJson(){

        JSONObject json;
        json = new JSONObject(this);

        return json.toString();

    }


    public String getDevelopment() {
        return development;
    }

    public void setDevelopment(String development) {
        if (development != null){
            if (!development.equals(this.development))
                this.development = development;
        }
        this.development = new String();
    }

    public String getSSL() {
        return SSL;
    }

    public void setSSL(String sSL) {
        if (sSL != null){
            if (!sSL.equals(this.SSL))
                this.SSL = sSL;
        }
        else
            this.SSL = new String();
    }

    public String getFeedback() {
        return feedback;
    }

    public void setFeedback(String feedback) {
        if (feedback != null){
            if (!feedback.equals(this.feedback))
                this.feedback = feedback;
        }
        this.feedback = new String();
    }

    public String getSandboxSSL() {
        return sandboxSSL;
    }

    public void setSandboxSSL(String sandboxSSL) {

        if (sandboxSSL != null){
            if (!sandboxSSL.equals(this.sandboxSSL))
                this.sandboxSSL = sandboxSSL;
        }else       
            this.sandboxSSL = new String();
    }

    public String getSandboxFeedback() {
        return sandboxFeedback;
    }

    public void setSandboxFeedback(String sandboxFeedback) {
        if (sandboxFeedback != null){
            if (!sandboxFeedback.equals(this.sandboxFeedback))
                this.sandboxFeedback = sandboxFeedback;
        }else
            this.sandboxFeedback = new String();
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        if (message != null){
            if (!message.equals(this.message))
                this.message = message;
        }else
            this.message = new String();
    }

    public String getEndPointArn() {
        return endPointArn;
    }

    public void setEndPointArn(String createPlatformEndpointResult) {
        if (createPlatformEndpointResult != null){
            if (!createPlatformEndpointResult.equals(this.endPointArn))
                this.endPointArn = createPlatformEndpointResult;
        }else
            this.endPointArn = new String();
    }

    public String getDeviceToken() {
        return deviceToken;
    }

    public void setDeviceToken(String deviceToken) {
        if (deviceToken != null){
            if (!deviceToken.equals(this.deviceToken))
                this.deviceToken = deviceToken;
        }else
            this.deviceToken = new String();
    }

    public EProduct getAppID() {
        return appID ;
    }

    public void setAppID(EProduct appID){
        if (appID != this.appID)
            this.appID = appID;
    }

    public String getAppVersion() {
        return appVersion;
    }

    public void setAppVersion(String appVersion) {
        if (appVersion != null){
            if (!appVersion.equals(this.appVersion))
                this.appVersion = appVersion;
        }else
            this.appVersion = new String();
    }

    public String getDeviceUID() {
        return deviceUID;
    }

    public void setDeviceUID(String deviceUID) {
        if (deviceUID != null){
            if (!deviceUID.equals(this.deviceUID))
                this.deviceUID = deviceUID;
        }else
            this.deviceUID = new String();
    }

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        if (deviceName != null){
            if (!deviceName.equals(this.deviceName))
                this.deviceName = deviceName;
        }else
            this.deviceName = new String();
    }

    public String getDeviceModel() {
        return deviceModel;
    }

    public void setDeviceModel(String deviceModel) {
        if (deviceModel != null){
            if (!deviceModel.equals(this.deviceModel))
                this.deviceModel = deviceModel;
        }else
            this.deviceModel = new String();
    }

    public String getDeviceVersion() {
        return deviceVersion;
    }

    public void setDeviceVersion(String deviceVersion) {
        if (deviceVersion != null){
            if (!deviceVersion.equals(this.deviceVersion))
                this.deviceVersion = deviceVersion;
        }else
            this.deviceVersion = new String();
    }

    public boolean isPushBadge() {
        return pushBadge;
    }

    public void setPushBadge(boolean pushBadge) {
        if (pushBadge != this.pushBadge)
            this.pushBadge = pushBadge;
    }

    public boolean isPushAlert() {
        return pushAlert;
    }

    public void setPushAlert(boolean pushAlert) {
        if (pushAlert != this.pushAlert)
            this.pushAlert = pushAlert;
    }

    public boolean isPushSound() {
        return pushSound;
    }

    public void setPushSound(boolean pushSound) {
        if (this.pushSound != pushSound)
            this.pushSound = pushSound;
    }

    public ESystem getSystem() {
        return system;
    }

    public void setTipoSmartphone(ESystem system) {
        if (system != this.system)
            this.system = system;
    }

    public EEnvironment getEnvironment() {
        return environment;
    }

    public void setEnvironment(EEnvironment environment) {
        if (environment != this.environment)
            this.environment = environment;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        if (this.id != id)
            this.id = id;
    }

    public void setSystem(ESystem system) {
        if (system != this.system)
    this.system = system;
    }

    public ArrayList<Subscription> getSubscriptions() {
        return subscriptions;
    }

    public void setSubscriptions(ArrayList<Subscription> subscriptions) {
        if (subscriptions != null)
            this.subscriptions = subscriptions;
        else
            this.subscriptions = new ArrayList<Subscription>();
    }

    public String getDeviceUser() {
        return deviceUser;
    }

    public void setDeviceUser(String deviceUser) {
        if (deviceUser != null){
            if (!deviceUser.equals(this.deviceUser))
                this.deviceUser = deviceUser;
        }else
            this.deviceUser = new String();
    }



}

这是Group类:

package com.neocloud.model;

import java.io.Serializable;
import java.util.ArrayList;

import com.amazonaws.util.json.JSONException;
import com.amazonaws.util.json.JSONObject;
import com.neocloud.amazon.AwsSns;
import com.neocloud.model.dao.GroupDAO;

public class Group implements Serializable{


    private static final long serialVersionUID = -5032857327241801763L;
    private long id;
    private String nome;
    private String topicArn;

    public Group(){
        this.clear();
    }

    public void clear(){
        this.id = 0;
        this.nome = new String();
        this.topicArn = new String();
    }

    public String registerGrupo(){

        AwsSns snsClient = AwsSns.getInstance();

        String topic = ""+snsClient.createTopic(this.nome);

        JSONObject json;
        String jsonResposta = topic;
        try {
            jsonResposta = jsonResposta.replace("}", "\"}").replace("arn", "\"arn");
            jsonResposta = jsonResposta.replace("{TopicArn:", "{\"TopicArn\":");
            json = new JSONObject(jsonResposta);
            setTopicArn(json.getString("TopicArn"));
        } catch (JSONException e) {

            e.printStackTrace();
        }

        GroupDAO grupoDAO = new GroupDAO();
        return grupoDAO.insert(this);

    }

    public String deleteGrupo(){
        AwsSns snsClient = AwsSns.getInstance();

        GroupDAO grupoDAO = new GroupDAO();
        ArrayList<Group> grupos = grupoDAO.select(this);

        if (grupos.size() > 0){

            for (int i = 0; i<grupos.size(); i++){
                this.topicArn = grupos.get(i).getTopicArn();
                this.nome = grupos.get(i).getNome();
                this.id = grupos.get(i).getId();
                snsClient.deleteTopic(this.topicArn);

            }

            return grupoDAO.delete(this);

        }

        return "-1";
    }

    public String toJson(){

        JSONObject json;
        json = new JSONObject(this);

        return json.toString();

    }

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getTopicArn() {
        return topicArn;
    }
    public void setTopicArn(String topicArn) {
        this.topicArn = topicArn;
    }

}

这是Tomcat日志:

  

线程中的异常&#34; http-bio-8080-exec-5&#34;   java.lang.StackOverflowError at   java.lang.reflect.Executable。(未知来源)at   java.lang.reflect.Method。(未知来源)at   java.lang.reflect.Method.copy(未知来源)at   java.lang.reflect.ReflectAccess.copyMethod(未知来源)at   sun.reflect.ReflectionFactory.copyMethod(未知来源)at   java.lang.Class.copyMethods(未知来源)at   java.lang.Class.getMethods(未知来源)at   com.amazonaws.util.json.JSONObject.populateMap(JSONObject.java:930)     在com.amazonaws.util.json.JSONObject。(JSONObject.java:285)at   com.amazonaws.util.json.JSONObject.wrap(JSONObject.java:1540)at at   com.amazonaws.util.json.JSONObject.populateMap(JSONObject.java:960)

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

可能是对象中的循环引用。

答案 1 :(得分:1)

json = new JSONObject(this);

你正在尝试JSONify这个

id = 0;
subscription = "";
grupo = new Group();
dispositivo = new Device();

我认为,不知何故,JSONify GroupDevice无法理解。你能告诉我们这些课程的代码吗?

更新:在Subscription.clear()方法中,您会影响new Group()&amp;一个new Device()。创建new Device()时,方法Device.clear()会被调用并创建new ArrayList<Subscription>(),该方法会调用方法Subscription.clear(),这会影响new Group() 1}}&amp;一个new Device()等等。您无限循环,这就是您收到StackOverflow错误的原因。很遗憾,您订阅的是一个由订阅的ArrayList组成的Group,它也是由Groups等组成的。

答案 2 :(得分:0)

也许你有一个循环参考......

Object a = X;
Object c = Y;

a.prop = Y;
c.prop2 = X;

然后,你的JSON(a)是

{
    prop : {
      prop2 : { 
       prop : Y (Y have prop2 again)
       /*infinite..*/
      }
    }
 }