json-as 0.6.1 → 0.6.3

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.
@@ -57,7 +57,7 @@ export namespace JSON {
57
57
  // @ts-ignore: Hidden function
58
58
  return data.__JSON_Serialize();
59
59
  } else if (data instanceof Date) {
60
- return data.toISOString();
60
+ return "\"" + data.toISOString() + "\"";
61
61
  } else if (isArrayLike<T>()) {
62
62
  // @ts-ignore
63
63
  if (data.length == 0) {
@@ -133,7 +133,7 @@ export namespace JSON {
133
133
  out = data.__JSON_Serialize();
134
134
  return;
135
135
  } else if (data instanceof Date) {
136
- out = data.toISOString();
136
+ out = "\"" + data.toISOString() + "\"";
137
137
  return;
138
138
  } else if (isArrayLike<T>()) {
139
139
  // @ts-ignore
@@ -716,94 +716,12 @@ export namespace JSON {
716
716
  return result;
717
717
  }
718
718
 
719
- // @ts-ignore: decorator
720
- @inline const
721
- MILLIS_PER_DAY = 1000 * 60 * 60 * 24,
722
- MILLIS_PER_HOUR = 1000 * 60 * 60,
723
- MILLIS_PER_MINUTE = 1000 * 60,
724
- MILLIS_PER_SECOND = 1000,
719
+ function parseDate(dateTimeString: string): Date {
720
+ // Use AssemblyScript's date parser
721
+ const d = Date.fromString(dateTimeString);
725
722
 
726
- YEARS_PER_EPOCH = 400,
727
- DAYS_PER_EPOCH = 146097,
728
- EPOCH_OFFSET = 719468, // Jan 1, 1970
729
- MILLIS_LIMIT = 8640000000000000;
730
-
731
- function parseDate(dateTimeString: string): Date {
732
- if (!dateTimeString.length) throw new RangeError(E_INVALIDDATE);
733
- var
734
- hour: i32 = 0,
735
- min: i32 = 0,
736
- sec: i32 = 0,
737
- ms: i32 = 0;
738
-
739
- let dateString = dateTimeString;
740
- let posT = dateTimeString.indexOf("T");
741
- if (~posT) {
742
- // includes a time component
743
- let timeString: string;
744
- dateString = dateTimeString.substring(0, posT);
745
- timeString = dateTimeString.substring(posT + 1);
746
- // parse the HH-MM-SS component
747
- let timeParts = timeString.split(":");
748
- let len = timeParts.length;
749
- if (len <= 1) throw new RangeError(E_INVALIDDATE);
750
-
751
- hour = i32.parse(timeParts[0]);
752
- min = i32.parse(timeParts[1]);
753
- if (len >= 3) {
754
- let secAndMs = timeParts[2];
755
- let posDot = secAndMs.indexOf(".");
756
- if (~posDot) {
757
- // includes milliseconds
758
- sec = i32.parse(secAndMs.substring(0, posDot));
759
- ms = i32.parse(secAndMs.substring(posDot + 1));
760
- } else {
761
- sec = i32.parse(secAndMs);
762
- }
763
- }
764
- }
765
- // parse the YYYY-MM-DD component
766
- let parts = dateString.split("-");
767
- let year = i32.parse(parts[0]);
768
- let month = 1, day = 1;
769
- let len = parts.length;
770
- if (len >= 2) {
771
- month = i32.parse(parts[1]);
772
- if (len >= 3) {
773
- day = i32.parse(parts[2]);
774
- }
775
- }
776
- return new Date(epochMillis(year, month, day, hour, min, sec, ms));
777
- }
778
-
779
- function epochMillis(
780
- year: i32,
781
- month: i32,
782
- day: i32,
783
- hour: i32,
784
- minute: i32,
785
- second: i32,
786
- milliseconds: i32
787
- ): i64 {
788
- return (
789
- daysSinceEpoch(year, month, day) * MILLIS_PER_DAY +
790
- hour * MILLIS_PER_HOUR +
791
- minute * MILLIS_PER_MINUTE +
792
- second * MILLIS_PER_SECOND +
793
- milliseconds
794
- );
723
+ // Return a new object instead of the one that the parser returned.
724
+ // This may seem redundant, but addreses the issue when Date
725
+ // is globally aliased to wasi_Date (or some other superclass).
726
+ return new Date(d.getTime());
795
727
  }
796
-
797
- function daysSinceEpoch(y: i32, m: i32, d: i32): i64 {
798
- y -= i32(m <= 2);
799
- let era = <u32>floorDiv(y, YEARS_PER_EPOCH);
800
- let yoe = <u32>y - era * YEARS_PER_EPOCH; // [0, 399]
801
- let doy = <u32>(153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1; // [0, 365]
802
- let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096]
803
- return <i64><i32>(era * 146097 + doe - EPOCH_OFFSET);
804
- }
805
-
806
- // @ts-ignore: decorator
807
- @inline function floorDiv<T extends number>(a: T, b: T): T {
808
- return (a - (a < 0 ? b - 1 : 0)) / b as T;
809
- }
package/assembly/test.ts CHANGED
@@ -44,7 +44,7 @@ console.log("Implemented: " + JSON.stringify(JSON.parse<Vec3>('{}', true)));
44
44
 
45
45
  console.log("Original: " + JSON.stringify(player));
46
46
  //console.log("Revised: " + vec.__JSON_Deserialize('{"x":3,"y":1,"z":8}').__JSON_Serialize());
47
- console.log("Implemented: " + JSON.stringify(JSON.parse<Player>('{"firstName":"Emmet","lastName":"West","lastActive":2023-11-16T04:06:35.108285303Z,"age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true}')));
47
+ console.log("Implemented: " + JSON.stringify(JSON.parse<Player>('{"firstName":"Emmet","lastName":"West","lastActive":"2023-11-16T04:06:35.108285303Z","age":23,"pos":{"x":3.4,"y":1.2,"z":8.3},"isVerified":true}')));
48
48
  /*
49
49
  // 9,325,755
50
50
  bench("Stringify Object (Vec3)", () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "0.6.1",
3
+ "version": "0.6.3",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "types": "assembly/index.ts",
6
6
  "author": "Jairus Tanaka",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-as/transform",
3
- "version": "0.6.1",
3
+ "version": "0.6.3",
4
4
  "description": "JSON encoder/decoder for AssemblyScript",
5
5
  "main": "./lib/index.js",
6
6
  "author": "Jairus Tanaka",