nodenetcdf 4.9.32 → 4.9.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodenetcdf",
3
- "version": "4.9.32",
3
+ "version": "4.9.33",
4
4
  "description": "Read and write NodeNetCDF files",
5
5
  "main": "./build/Release/nodenetcdf.node",
6
6
  "types": "./index.d.ts",
package/src/File.cpp CHANGED
@@ -174,11 +174,27 @@ void File::ToJSON(const v8::FunctionCallbackInfo<v8::Value> &args)
174
174
  v8::Isolate *isolate = args.GetIsolate();
175
175
  v8::Local<v8::Context> context = isolate->GetCurrentContext();
176
176
 
177
- // Get the root group and serialize it
177
+ // Get the root group
178
178
  v8::Local<v8::String> rootProp = v8::String::NewFromUtf8(isolate, "root", v8::NewStringType::kNormal).ToLocalChecked();
179
179
  v8::Local<v8::Value> root = args.Holder()->Get(context, rootProp).ToLocalChecked();
180
180
 
181
- // The root group's toJSON will automatically serialize all children recursively
181
+ // Call toJSON on the root group to get proper array conversion
182
+ if (root->IsObject())
183
+ {
184
+ v8::Local<v8::Object> rootObj = root->ToObject(context).ToLocalChecked();
185
+ v8::Local<v8::String> toJSONProp = v8::String::NewFromUtf8(isolate, "toJSON", v8::NewStringType::kNormal).ToLocalChecked();
186
+ v8::Local<v8::Value> toJSONMethod = rootObj->Get(context, toJSONProp).ToLocalChecked();
187
+
188
+ if (toJSONMethod->IsFunction())
189
+ {
190
+ v8::Local<v8::Function> toJSONFunc = v8::Local<v8::Function>::Cast(toJSONMethod);
191
+ v8::Local<v8::Value> result = toJSONFunc->Call(context, rootObj, 0, nullptr).ToLocalChecked();
192
+ args.GetReturnValue().Set(result);
193
+ return;
194
+ }
195
+ }
196
+
197
+ // Fallback: return root as-is
182
198
  args.GetReturnValue().Set(root);
183
199
  }
184
200
  } // namespace nodenetcdfjs
package/src/Group.cpp CHANGED
@@ -525,69 +525,121 @@ void Group::ToJSON(const v8::FunctionCallbackInfo<v8::Value> &args)
525
525
  }
526
526
  }
527
527
 
528
- // Convert dimensions object to array
528
+ // Convert dimensions object to array, calling toJSON on each item
529
529
  v8::Local<v8::Value> dimensions = args.Holder()->Get(context, dimensions_str).ToLocalChecked();
530
530
  if (dimensions->IsObject() && !dimensions->IsNull())
531
531
  {
532
532
  v8::Local<v8::Object> dimsObj = dimensions->ToObject(context).ToLocalChecked();
533
533
  v8::Local<v8::Array> propNames = dimsObj->GetOwnPropertyNames(context).ToLocalChecked();
534
534
  v8::Local<v8::Array> dimsArray = v8::Array::New(isolate, propNames->Length());
535
+ v8::Local<v8::String> toJSONStr = v8::String::NewFromUtf8Literal(isolate, "toJSON");
535
536
 
536
537
  for (uint32_t i = 0; i < propNames->Length(); i++)
537
538
  {
538
539
  v8::Local<v8::Value> key = propNames->Get(context, i).ToLocalChecked();
539
540
  v8::Local<v8::Value> value = dimsObj->Get(context, key).ToLocalChecked();
541
+
542
+ // Call toJSON if available to ensure proper serialization
543
+ if (value->IsObject())
544
+ {
545
+ v8::Local<v8::Object> valueObj = value->ToObject(context).ToLocalChecked();
546
+ v8::Local<v8::Value> toJSON = valueObj->Get(context, toJSONStr).ToLocalChecked();
547
+ if (toJSON->IsFunction())
548
+ {
549
+ v8::Local<v8::Function> toJSONFunc = v8::Local<v8::Function>::Cast(toJSON);
550
+ value = toJSONFunc->Call(context, valueObj, 0, nullptr).ToLocalChecked();
551
+ }
552
+ }
540
553
  (void)dimsArray->Set(context, i, value);
541
554
  }
542
555
  (void)json->CreateDataProperty(context, dimensions_str, dimsArray);
543
556
  }
544
557
 
545
- // Convert variables object to array
558
+ // Convert variables object to array, calling toJSON on each item
546
559
  v8::Local<v8::Value> variables = args.Holder()->Get(context, variables_str).ToLocalChecked();
547
560
  if (variables->IsObject() && !variables->IsNull())
548
561
  {
549
562
  v8::Local<v8::Object> varsObj = variables->ToObject(context).ToLocalChecked();
550
563
  v8::Local<v8::Array> propNames = varsObj->GetOwnPropertyNames(context).ToLocalChecked();
551
564
  v8::Local<v8::Array> varsArray = v8::Array::New(isolate, propNames->Length());
565
+ v8::Local<v8::String> toJSONStr = v8::String::NewFromUtf8Literal(isolate, "toJSON");
552
566
 
553
567
  for (uint32_t i = 0; i < propNames->Length(); i++)
554
568
  {
555
569
  v8::Local<v8::Value> key = propNames->Get(context, i).ToLocalChecked();
556
570
  v8::Local<v8::Value> value = varsObj->Get(context, key).ToLocalChecked();
571
+
572
+ // Call toJSON if available to ensure proper serialization
573
+ if (value->IsObject())
574
+ {
575
+ v8::Local<v8::Object> valueObj = value->ToObject(context).ToLocalChecked();
576
+ v8::Local<v8::Value> toJSON = valueObj->Get(context, toJSONStr).ToLocalChecked();
577
+ if (toJSON->IsFunction())
578
+ {
579
+ v8::Local<v8::Function> toJSONFunc = v8::Local<v8::Function>::Cast(toJSON);
580
+ value = toJSONFunc->Call(context, valueObj, 0, nullptr).ToLocalChecked();
581
+ }
582
+ }
557
583
  (void)varsArray->Set(context, i, value);
558
584
  }
559
585
  (void)json->CreateDataProperty(context, variables_str, varsArray);
560
586
  }
561
587
 
562
- // Convert attributes object to array
588
+ // Convert attributes object to array, calling toJSON on each item
563
589
  v8::Local<v8::Value> attributes = args.Holder()->Get(context, attributes_str).ToLocalChecked();
564
590
  if (attributes->IsObject() && !attributes->IsNull())
565
591
  {
566
592
  v8::Local<v8::Object> attrsObj = attributes->ToObject(context).ToLocalChecked();
567
593
  v8::Local<v8::Array> propNames = attrsObj->GetOwnPropertyNames(context).ToLocalChecked();
568
594
  v8::Local<v8::Array> attrsArray = v8::Array::New(isolate, propNames->Length());
595
+ v8::Local<v8::String> toJSONStr = v8::String::NewFromUtf8Literal(isolate, "toJSON");
569
596
 
570
597
  for (uint32_t i = 0; i < propNames->Length(); i++)
571
598
  {
572
599
  v8::Local<v8::Value> key = propNames->Get(context, i).ToLocalChecked();
573
600
  v8::Local<v8::Value> value = attrsObj->Get(context, key).ToLocalChecked();
601
+
602
+ // Call toJSON if available to ensure proper serialization
603
+ if (value->IsObject())
604
+ {
605
+ v8::Local<v8::Object> valueObj = value->ToObject(context).ToLocalChecked();
606
+ v8::Local<v8::Value> toJSON = valueObj->Get(context, toJSONStr).ToLocalChecked();
607
+ if (toJSON->IsFunction())
608
+ {
609
+ v8::Local<v8::Function> toJSONFunc = v8::Local<v8::Function>::Cast(toJSON);
610
+ value = toJSONFunc->Call(context, valueObj, 0, nullptr).ToLocalChecked();
611
+ }
612
+ }
574
613
  (void)attrsArray->Set(context, i, value);
575
614
  }
576
615
  (void)json->CreateDataProperty(context, attributes_str, attrsArray);
577
616
  }
578
617
 
579
- // Convert subgroups object to array
618
+ // Convert subgroups object to array, calling toJSON on each item
580
619
  v8::Local<v8::Value> subgroups = args.Holder()->Get(context, subgroups_str).ToLocalChecked();
581
620
  if (subgroups->IsObject() && !subgroups->IsNull())
582
621
  {
583
622
  v8::Local<v8::Object> subgrpsObj = subgroups->ToObject(context).ToLocalChecked();
584
623
  v8::Local<v8::Array> propNames = subgrpsObj->GetOwnPropertyNames(context).ToLocalChecked();
585
624
  v8::Local<v8::Array> subgrpsArray = v8::Array::New(isolate, propNames->Length());
625
+ v8::Local<v8::String> toJSONStr = v8::String::NewFromUtf8Literal(isolate, "toJSON");
586
626
 
587
627
  for (uint32_t i = 0; i < propNames->Length(); i++)
588
628
  {
589
629
  v8::Local<v8::Value> key = propNames->Get(context, i).ToLocalChecked();
590
630
  v8::Local<v8::Value> value = subgrpsObj->Get(context, key).ToLocalChecked();
631
+
632
+ // Call toJSON if available to ensure proper serialization
633
+ if (value->IsObject())
634
+ {
635
+ v8::Local<v8::Object> valueObj = value->ToObject(context).ToLocalChecked();
636
+ v8::Local<v8::Value> toJSON = valueObj->Get(context, toJSONStr).ToLocalChecked();
637
+ if (toJSON->IsFunction())
638
+ {
639
+ v8::Local<v8::Function> toJSONFunc = v8::Local<v8::Function>::Cast(toJSON);
640
+ value = toJSONFunc->Call(context, valueObj, 0, nullptr).ToLocalChecked();
641
+ }
642
+ }
591
643
  (void)subgrpsArray->Set(context, i, value);
592
644
  }
593
645
  (void)json->CreateDataProperty(context, subgroups_str, subgrpsArray);
package/src/Variable.cpp CHANGED
@@ -1383,25 +1383,59 @@ void Variable::ToJSON(const v8::FunctionCallbackInfo<v8::Value> &args)
1383
1383
  (void)json->CreateDataProperty(context, type_str,
1384
1384
  v8::String::NewFromUtf8(isolate, type_name, v8::NewStringType::kInternalized).ToLocalChecked());
1385
1385
 
1386
- // Dimensions are already an array, just add them
1386
+ // Dimensions are already an array, but call toJSON on each item
1387
1387
  v8::Local<v8::Value> dimensions = args.Holder()->Get(context, dimensions_str).ToLocalChecked();
1388
1388
  if (dimensions->IsArray())
1389
1389
  {
1390
- (void)json->CreateDataProperty(context, dimensions_str, dimensions);
1390
+ v8::Local<v8::Array> dimsArray = v8::Local<v8::Array>::Cast(dimensions);
1391
+ v8::Local<v8::Array> newDimsArray = v8::Array::New(isolate, dimsArray->Length());
1392
+ v8::Local<v8::String> toJSONStr = v8::String::NewFromUtf8Literal(isolate, "toJSON");
1393
+
1394
+ for (uint32_t i = 0; i < dimsArray->Length(); i++)
1395
+ {
1396
+ v8::Local<v8::Value> value = dimsArray->Get(context, i).ToLocalChecked();
1397
+
1398
+ // Call toJSON if available to ensure proper serialization
1399
+ if (value->IsObject())
1400
+ {
1401
+ v8::Local<v8::Object> valueObj = value->ToObject(context).ToLocalChecked();
1402
+ v8::Local<v8::Value> toJSON = valueObj->Get(context, toJSONStr).ToLocalChecked();
1403
+ if (toJSON->IsFunction())
1404
+ {
1405
+ v8::Local<v8::Function> toJSONFunc = v8::Local<v8::Function>::Cast(toJSON);
1406
+ value = toJSONFunc->Call(context, valueObj, 0, nullptr).ToLocalChecked();
1407
+ }
1408
+ }
1409
+ (void)newDimsArray->Set(context, i, value);
1410
+ }
1411
+ (void)json->CreateDataProperty(context, dimensions_str, newDimsArray);
1391
1412
  }
1392
1413
 
1393
- // Convert attributes object to array
1414
+ // Convert attributes object to array, calling toJSON on each item
1394
1415
  v8::Local<v8::Value> attributes = args.Holder()->Get(context, attributes_str).ToLocalChecked();
1395
1416
  if (attributes->IsObject() && !attributes->IsNull())
1396
1417
  {
1397
1418
  v8::Local<v8::Object> attrsObj = attributes->ToObject(context).ToLocalChecked();
1398
1419
  v8::Local<v8::Array> propNames = attrsObj->GetOwnPropertyNames(context).ToLocalChecked();
1399
1420
  v8::Local<v8::Array> attrsArray = v8::Array::New(isolate, propNames->Length());
1421
+ v8::Local<v8::String> toJSONStr = v8::String::NewFromUtf8Literal(isolate, "toJSON");
1400
1422
 
1401
1423
  for (uint32_t i = 0; i < propNames->Length(); i++)
1402
1424
  {
1403
1425
  v8::Local<v8::Value> key = propNames->Get(context, i).ToLocalChecked();
1404
1426
  v8::Local<v8::Value> value = attrsObj->Get(context, key).ToLocalChecked();
1427
+
1428
+ // Call toJSON if available to ensure proper serialization
1429
+ if (value->IsObject())
1430
+ {
1431
+ v8::Local<v8::Object> valueObj = value->ToObject(context).ToLocalChecked();
1432
+ v8::Local<v8::Value> toJSON = valueObj->Get(context, toJSONStr).ToLocalChecked();
1433
+ if (toJSON->IsFunction())
1434
+ {
1435
+ v8::Local<v8::Function> toJSONFunc = v8::Local<v8::Function>::Cast(toJSON);
1436
+ value = toJSONFunc->Call(context, valueObj, 0, nullptr).ToLocalChecked();
1437
+ }
1438
+ }
1405
1439
  (void)attrsArray->Set(context, i, value);
1406
1440
  }
1407
1441
  (void)json->CreateDataProperty(context, attributes_str, attrsArray);
package/test/json.js CHANGED
@@ -164,11 +164,15 @@ describe('JSON Serialization', function() {
164
164
  it('should recursively serialize entire file structure', function() {
165
165
  var json = JSON.parse(JSON.stringify(file));
166
166
 
167
- // File should serialize as root group with all children
167
+ // File should serialize as root group with all children as arrays
168
168
  expect(json).to.have.property('dimensions');
169
169
  expect(json).to.have.property('variables');
170
- expect(json.variables).to.be.an('object');
171
- expect(json.dimensions).to.be.an('object');
170
+ expect(json).to.have.property('attributes');
171
+ expect(json).to.have.property('subgroups');
172
+ expect(json.variables).to.be.an('array');
173
+ expect(json.dimensions).to.be.an('array');
174
+ expect(json.attributes).to.be.an('array');
175
+ expect(json.subgroups).to.be.an('array');
172
176
  });
173
177
  });
174
178
  });