5
5
import { Changes } from "../changes" ;
6
6
import { TripleIndex } from "../indexes" ;
7
7
8
+ const JSON_NULL_ID = "external|json|null" ;
9
+
8
10
//---------------------------------------------------------------------
9
11
// JS conversion
10
12
//---------------------------------------------------------------------
11
13
12
14
export function fromJS ( changes : Changes , json : any , node : string , scope : string , idPrefix : string = "js" ) {
15
+ if ( json === null || json === undefined ) return JSON_NULL_ID ;
13
16
if ( json . constructor === Array ) {
14
17
let arrayId = `${ idPrefix } |array` ;
15
18
changes . store ( scope , arrayId , "tag" , "array" , node ) ;
@@ -26,10 +29,15 @@ export function fromJS(changes: Changes, json: any, node: string, scope: string,
26
29
let objectId = `${ idPrefix } |object` ;
27
30
for ( let key of Object . keys ( json ) ) {
28
31
let value = json [ key ] ;
29
- if ( value !== null && ( value . constructor === Array || typeof value === "object" ) ) {
30
- value = fromJS ( changes , value , node , scope , `${ objectId } |${ key } ` ) ;
32
+ if ( value === null || value === undefined ) {
33
+ changes . store ( scope , JSON_NULL_ID , "tag" , "json-null" , node ) ;
34
+ changes . store ( scope , objectId , key , JSON_NULL_ID , node ) ;
35
+ } else {
36
+ if ( value . constructor === Array || typeof value === "object" ) {
37
+ value = fromJS ( changes , value , node , scope , `${ objectId } |${ key } ` ) ;
38
+ }
39
+ changes . store ( scope , objectId , key , value , node ) ;
31
40
}
32
- changes . store ( scope , objectId , key , value , node ) ;
33
41
}
34
42
return objectId ;
35
43
} else {
@@ -39,6 +47,7 @@ export function fromJS(changes: Changes, json: any, node: string, scope: string,
39
47
40
48
export function toJS ( index : TripleIndex , recordId ) {
41
49
let result ;
50
+ if ( recordId === JSON_NULL_ID ) return null ;
42
51
let isArray = index . lookup ( recordId , "tag" , "array" ) ;
43
52
if ( isArray !== undefined ) {
44
53
result = [ ] ;
0 commit comments