상세 컨텐츠

본문 제목

[Unity] 유니티 JSONObject 정수형 엄청큰수를 받을때 값이 안맞는 버그 관련

게임엔진관련/유니티 엔진

by AlrepondTech 2020. 4. 29. 01:39

본문

반응형

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

이 현상은 코드버전에 따라 버그가 없을 수도 있다.


JSONObject 에서 정수형값을 받을때

[예)obj.GetField("pnt").n; ]  여기에서 n의 값을 float로 해서 받게되는데, 여기에서 숫자가 엄청 큰수가 

들어오면 값이 제대로 맞지 않는 버그가 있다. 이버그는 셋팅을 "double"로 받는설정으로 바꾸면

해결이 된다.

 

[예)obj.GetField("pnt").n; ]  여기에서 VS2019 기준으로 ctrl키를 눌러주어서 예)부분의 ".n"을 눌러주어

"JSONObject클래스"로 들어간다. 거기서 보면 #if USEFLOAT{...} 와 else {...} 부분으로 나누어지는데

우리는 else {...}부분을 쓰도록 해야한다 "JSONObject클래스"맨 윗부분에 "#define USEFLOAT" 라고

선언이 되어있는데 이부분을 주석처리 해주자

 

위 그림과 같이 빨간색 화살표 밑줄 부분을 주석처리 해주면된다.

주석처리 해주면 정수값 "float"로 받던 것을 "double"로 받게 된다. 그리고 바꾸면 다른 "JSONObject클래스" 관련 연동 클래스에서 에러가 나는데, "float"로 사용해야만 하는 것들은 옆에"(float)"로 붙여주어서 캐스팅 해주면 된다.

 

그리고 "JSONObject클래스" 부분에 "AddField(...)" 부분에 double 받는 부분도 추가해주어야 한다.

//----------------------------------------------------------------------------------------------

public static JSONObject Create(double val) //fix-psj
{

    JSONObject obj = Create();

    obj.type = Type.NUMBER;

    obj.n = val;

    return obj;

}

 

public void AddField(string name, double val){
    AddField(name, Create(val));
}

//----------------------------------------------------------------------------------------------

이 부분을 넣어주자 "public static JSONObject Create(float val)", "AddField(string name, float val)" 부분이 제대로 값을 못받는 경우가 있어 필요가 없으면 지워주고 위의 함수를 넣어주면 된다.

 

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

출처: https://github.com/stleary/JSON-java/issues/42

 

Hi Douglas,

Just wanted to point out a bug in your otherwise wonderful code.

Currently, putting float values into a JSONObject is using the put(String, double) method.
Unfortunately, this type of cast causes floating point precision problems.
For example, the float number 6.99 is translated to the double 6.989999771118164

To solve this issue I added the following method to the JSONObject:

 

/** * Put a key/double pair in the JSONObject. * * @param key A key string. * @param value A float which is the value. * @return this. * @throws JSONException If the key is null or if the number is invalid. */

public JSONObject put(String key, float value) throws JSONException

{

    this.put(key, Double.parseDouble(String.valueOf(value)));

     return this;

}

 

Hopefully you could add this bug fix to your code.

Guy

 

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

기타관련링크

- https://hoonheui.tistory.com/entry/JSON-Object-floating-%EC%97%B0%EC%82%B0-%EC%98%A4%EB%A5%98-%ED%95%B4%EA%B2%B0

 

반응형


관련글 더보기

댓글 영역