Skip to content

Commit

Permalink
1. Fix #13 for #12 regression bug on toJson.
Browse files Browse the repository at this point in the history
2. Bump 3.0.1
  • Loading branch information
jumperchen committed Oct 25, 2024
1 parent 30a9784 commit 54c582c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 48 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.0.1

**Bug fix:**
* [socket_io_common#13](https://github.com/rikulo/socket_io_common/pull/13) Fix #12 regression bug on toJson.

## 3.0.0

**Bug fix:**
Expand Down
18 changes: 18 additions & 0 deletions lib/src/parser/is_binary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ bool hasBinary(obj, [bool toJSON = false]) {
}

if (obj is Map) {
if (obj['toJSON'] != null && obj['toJSON'] is Function && toJSON == false) {
return hasBinary(obj['toJSON'](), true);
} else if (obj['toJson'] != null &&
obj['toJson'] is Function &&
toJSON == false) {
return hasBinary(obj['toJson'](), true);
}
for (var entry in obj.entries) {
if (hasBinary(entry.value)) {
return true;
Expand All @@ -50,12 +57,23 @@ bool hasBinary(obj, [bool toJSON = false]) {

// Helper function to dynamically check if an object has a toJSON method
Function? _getToJsonMethod(obj) {
var toJsonMethod = null;
try {
// backwards compatibility for toJSON method
var toJsonMethod = obj.toJSON;
if (toJsonMethod is Function) {
return toJsonMethod;
}
} catch (e) {
try {
// from dart-lang json.dart, the method is called toJson
toJsonMethod = obj.toJson;
if (toJsonMethod is Function) {
return toJsonMethod;
}
} catch (e) {
// Catch and ignore errors if the method is not present
}
// Catch and ignore errors if the method is not present
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: socket_io_common
description: Socket.io common parser library.
version: 3.0.0
version: 3.0.1
homepage: https://www.zkoss.org
repository: https://github.com/rikulo/socket_io_common
issue_tracker: https://github.com/rikulo/socket_io_common/issues
Expand Down
121 changes: 74 additions & 47 deletions test/isBinary.test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:typed_data';

import 'package:socket_io_common/src/parser/is_binary.dart';
import 'package:test/test.dart';

createMap(TypedData? byteData) => {
"child": byteData,
Expand Down Expand Up @@ -41,53 +42,79 @@ createMapWithToJson(TypedData? byteData) => {
main() {
final byteData = ByteData(1);

// -------------
print("With ByteBuffer: ${hasBinary(byteData.buffer)}");
print("With ByteData: ${hasBinary(byteData)}");
print("-" * 30);
// -------------
print("With map and binary: ${hasBinary(createMap(byteData))}");
print("With map and null: ${hasBinary(createMap(null))}");
print("-" * 30);
// -------------
print(
"With map with array and binary: ${hasBinary(createMapWithArray(byteData))}");
print("With map with array and null: ${hasBinary(createMapWithArray(null))}");
print("-" * 30);
// -------------
print("With deep map and binary: ${hasBinary(createDeepMap(byteData))}");
print("With deep map and null: ${hasBinary(createDeepMap(null))}");
print("-" * 30);
// -------------
print(
"With deep map with array and binary: ${hasBinary(createDeepMapWithArray(byteData))}");
print(
"With deep map with array and null: ${hasBinary(createDeepMapWithArray(null))}");
print("-" * 30);
// -------------
print("With array and binary: ${hasBinary(createArray(byteData))}");
print("With array and null: ${hasBinary(createArray(null))}");
print("-" * 30);
// -------------
print(
"With array in array and binary: ${hasBinary(createArrayInArray(byteData))}");
print("With array in array and null: ${hasBinary(createArrayInArray(null))}");
print("-" * 30);
// -------------
print(
"With array with map and binary: ${hasBinary(createArrayWithMap(byteData))}");
print("With array with map and null: ${hasBinary(createArrayWithMap(null))}");
print("-" * 30);
// -------------
print(
"With array with deep map and binary: ${hasBinary(createArrayWithDeepMap(byteData))}");
print(
"With array with deep map and null: ${hasBinary(createArrayWithDeepMap(null))}");
print("-" * 30);
// -------------
print("With toJSON and binary: ${hasBinary(createMapWithToJson(byteData))}");
print("With toJSON and null: ${hasBinary(createMapWithToJson(null))}");
print("With toJSON from an Object: ${hasBinary(MyObject())}");
test("With ByteBuffer:", () {
expect(hasBinary(byteData.buffer), true);
});

test("With ByteData:", () {
expect(hasBinary(byteData), true);
});

test("With map and binary:", () {
expect(hasBinary(createMap(byteData)), true);
});
test("With map and null:", () {
expect(hasBinary(createMap(null)), false);
});

test("With map with array and binary:", () {
expect(hasBinary(createMapWithArray(byteData)), true);
});
test("With map with array and null:", () {
expect(hasBinary(createMapWithArray(null)), false);
});

test("With deep map and binary:", () {
expect(hasBinary(createDeepMap(byteData)), true);
});
test("With deep map and null:", () {
expect(hasBinary(createDeepMap(null)), false);
});

test("With deep map with array and binary:", () {
expect(hasBinary(createDeepMapWithArray(byteData)), true);
});
test("With deep map with array and null:", () {
expect(hasBinary(createDeepMapWithArray(null)), false);
});

test("With array and binary:", () {
expect(hasBinary(createArray(byteData)), true);
});
test("With array and null:", () {
expect(hasBinary(createArray(null)), false);
});

test("With array in array and binary:", () {
expect(hasBinary(createArrayInArray(byteData)), true);
});
test("With array in array and null:", () {
expect(hasBinary(createArrayInArray(null)), false);
});

test("With array with map and binary:", () {
expect(hasBinary(createArrayWithMap(byteData)), true);
});
test("With array with map and null:", () {
expect(hasBinary(createArrayWithMap(null)), false);
});

test("With array with deep map and binary:", () {
expect(hasBinary(createArrayWithDeepMap(byteData)), true);
});
test("With array with deep map and null:", () {
expect(hasBinary(createArrayWithDeepMap(null)), false);
});

test("With toJSON and binary:", () {
expect(hasBinary(createMapWithToJson(byteData)), true);
});
test("With toJSON and null:", () {
expect(hasBinary(createMapWithToJson(null)), false);
});
test("With toJSON from an Object:", () {
expect(hasBinary(MyObject()), true);
});
}

class MyObject {
Expand Down

0 comments on commit 54c582c

Please sign in to comment.