Skip to content

Commit 50d35ea

Browse files
committed
json, feat: support encode BigInt.
1 parent 9467776 commit 50d35ea

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed

fibjs/include/Buffer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class Buffer : public Buffer_base {
112112
virtual result_t toJSON(exlib::string key, v8::Local<v8::Value>& retVal);
113113

114114
public:
115-
static result_t fromJSON(Isolate* isolate, v8::Local<v8::Value> data, v8::Local<v8::Object>& o);
115+
static result_t fromJSON(Isolate* isolate, v8::Local<v8::Value> data, v8::Local<v8::Value>& o);
116116

117117
char* data()
118118
{

fibjs/src/base/Runtime.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,17 @@ Isolate* Isolate::current()
312312
return rt->isolate();
313313
}
314314

315+
static void BigInt_toJSON(const v8::FunctionCallbackInfo<v8::Value>& args)
316+
{
317+
v8::Isolate* isolate = args.GetIsolate();
318+
v8::Local<v8::Object> o = v8::Object::New(isolate);
319+
320+
o->Set(NewString(isolate, "type", 4), NewString(isolate, "BigInt", 6));
321+
o->Set(NewString(isolate, "data", 4), args.This()->ToString(isolate));
322+
323+
args.GetReturnValue().Set(o);
324+
}
325+
315326
void Isolate::init()
316327
{
317328
s_isolates.putTail(this);
@@ -332,6 +343,9 @@ void Isolate::init()
332343

333344
_context->SetEmbedderData(1, v8::Object::New(m_isolate)->GetPrototype());
334345

346+
v8::Local<v8::Object> proto = v8::Local<v8::Object>::Cast(v8::Local<v8::Object>::Cast(v8::BigIntObject::New(m_isolate, 0))->GetPrototype());
347+
proto->Set(NewString("toJSON"), NewFunction("toJSON", BigInt_toJSON));
348+
335349
static const char* skips[] = { "Master", "repl", "argv", "__filename", "__dirname", NULL };
336350
global_base::class_info().Attach(this, _context->Global(), skips);
337351

fibjs/src/encoding/encoding_json.cpp

+19-4
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,26 @@ namespace fibjs {
1616

1717
DECLARE_MODULE(json);
1818

19+
result_t BigInt_fromJSON(Isolate* isolate, v8::Local<v8::Value> data, v8::Local<v8::Value>& retVal)
20+
{
21+
TryCatch try_catch;
22+
23+
v8::MaybeLocal<v8::BigInt> v = data->ToBigInt(isolate->context());
24+
try_catch.Reset();
25+
26+
if (v.IsEmpty())
27+
return CALL_E_TYPEMISMATCH;
28+
29+
retVal = v.ToLocalChecked();
30+
return 0;
31+
}
32+
1933
struct _from {
2034
const char* name;
21-
result_t (*from)(Isolate*, v8::Local<v8::Value>, v8::Local<v8::Object>&);
35+
result_t (*from)(Isolate*, v8::Local<v8::Value>, v8::Local<v8::Value>&);
2236
} s_from[] = {
2337
{ "Buffer", Buffer::fromJSON },
38+
{ "BigInt", BigInt_fromJSON },
2439
{ NULL }
2540
};
2641

@@ -315,9 +330,9 @@ inline result_t _jsonDecode(exlib::string data,
315330

316331
for (i = 0; s_from[i].name; i++)
317332
if (!qstrcmp(*str, s_from[i].name)) {
318-
hr = s_from[i].from(isolate, data, json_object);
319-
if (hr < 0)
320-
return hr;
333+
hr = s_from[i].from(isolate, data, retVal);
334+
if (hr >= 0)
335+
return 0;
321336
break;
322337
}
323338
}

fibjs/src/global/Buffer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ result_t Buffer::toJSON(exlib::string key, v8::Local<v8::Value>& retVal)
11561156
return 0;
11571157
}
11581158

1159-
result_t Buffer::fromJSON(Isolate* isolate, v8::Local<v8::Value> data, v8::Local<v8::Object>& o)
1159+
result_t Buffer::fromJSON(Isolate* isolate, v8::Local<v8::Value> data, v8::Local<v8::Value>& o)
11601160
{
11611161
result_t hr;
11621162
v8::Local<v8::Array> arr;

test/encoding_test.js

+36
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ describe('encoding', () => {
254254

255255
assert.equal(json.encode(json.decode('{"a":100,"b":200}')),
256256
'{"a":100,"b":200}');
257+
});
257258

259+
it('json encode object', () => {
258260
var buf = new Buffer('test');
259261
var j = json.encode(buf);
260262
assert.equal(j, '{"type":"Buffer","data":[116,101,115,116]}');
@@ -272,6 +274,40 @@ describe('encoding', () => {
272274
}));
273275

274276
assert.deepEqual(buf, buf1);
277+
278+
var o = {
279+
"type": "Buffer",
280+
"data": {
281+
a: 100,
282+
b: 200
283+
}
284+
};
285+
assert.deepEqual(json.decode(json.encode(o)), o)
286+
287+
var o = {
288+
bigint: 10000n
289+
};
290+
291+
var j = json.encode(o);
292+
assert.equal(j, '{"bigint":{"type":"BigInt","data":"10000"}}')
293+
294+
var o1 = json.decode(j);
295+
assert.deepEqual(o, o1);
296+
297+
var o = {
298+
"type": "BigInt",
299+
"data": "aaaaa"
300+
};
301+
assert.deepEqual(json.decode(json.encode(o)), o)
302+
303+
var o = {
304+
"type": "BigInt",
305+
"data": {
306+
a: 100,
307+
b: 200
308+
}
309+
};
310+
assert.deepEqual(json.decode(json.encode(o)), o)
275311
});
276312

277313
it('json encode error when circular', () => {

0 commit comments

Comments
 (0)