json cpp longlong 型別的擴充套件

2021-08-26 22:51:38 字數 1345 閱讀 4872

用cocos2dx做跨平台專案的時候,自然就用了c++版本的json庫,這個json-cpp是用的比較多的,總體用起來還算可以,有乙個很不方便的地方就是不支援long long(int64)型別,一開始我使用double型別來強制轉換,後來出了bug,double精度只有16位,超過20位的就會被四捨五入,試過了官網上bug列表裡的方法都不行,

沒辦法,花點時間新增longvalue才解決這個問題,加地方實在太多了,附上主要擴充套件**備忘:

一,定義乙個long型別

//lancer add long

typedeflonglonglong;

二、//型別裡新增longvalue

enum valuetype

;三、新增乙個解析long型別的方法

bool

reader::decodelonglong( token &token )

else

if ( count != 1 )

return

adderror( "'" + std::string( token.start_, token.end_ ) + "' is not a number.", token );

currentvalue() = value;

returntrue; }

四、解析數字

bool

reader::decodenumber( token &token )方法裡,超出int型資料範圍的時候使用decodelonglong方法,而不是之前的decodedouble

while ( current < token.end_ )

五,新增乙個aslonglong方法供呼叫

//lancer as long

long

value::aslonglong() const

case

arrayvalue:

case

objectvalue:

json_assert_message

( false

, "type is not convertible to int");

default:

json_assert_unreachable; }

return

0; // unreachable;

}其他地方細節要改的太多了,我都是全域性搜尋realvalue,然後對應的地方改掉

另外,由於後台傳輸資料的時候數字可能會被傳成字串,所以解析數字的地方都需要加上對字串的判斷,

比如:value::int

value::asint() const

作者(author):smilelance

出處( from ):

將基於Nullable的型別轉換實現在擴充套件方法中

從上面的介紹我們可以得出這樣的結論 如果型別t1和t2能夠相互相容,我們可以借助convert將t1型別物件轉換成t2型別,然後通過顯式型別轉換進一步轉換成nullable。我們可以通過這兩個步驟實現針對於nullable型別的轉換。為了操作方便,我將此轉換邏輯寫在針對iconvertible介面的...

利用 where 條件給特定的集合型別增加擴充套件方法

有時我們需要給特定的集合型別增加乙個擴充套件方法,例如給 array 增加乙個 isstringelement 屬性,或者增加乙個 random 方法來實現 string 的隨機亂序,這時可以使用 where 子句,限制 element 為 string,例如 extension array whe...

Easyui 基於kindeditor的擴充套件

原始碼 author 夏悸 easyui kindeditor的簡單擴充套件.有了這個之後,你就可以像使用easyui元件的方式使用kindeditor了 前提是你需要匯入kindeditor的核心js和相關樣式.本外掛程式也需要在easyui.min和kindeditor之後匯入.呵呵.沒做深入擴...