Skip to content

Commit 550719c

Browse files
author
pkafel
committed
#2 Support for nulls
1 parent 99111f0 commit 550719c

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

json-diff.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ REMOVE = "REMOVE";
77

88
// TYPES
99
ARRAY = "ARRAY";
10+
NULL = "NULL";
1011
OBJECT = "OBJECT";
1112
SCALAR = "SCALAR";
1213

@@ -32,6 +33,7 @@ function ValidationException(leftError, rightError) {
3233
function getDiffRepresentation(left, right) {
3334

3435
function _getType(v) {
36+
if(v === null) return NULL;
3537
var type = typeof(v);
3638
if (type === 'number' || type === 'string' || type === 'boolean') return SCALAR;
3739
if (type === 'object') {
@@ -102,8 +104,10 @@ function getDiffRepresentation(left, right) {
102104
result = result.concat(_getScalarsDiff(null, left[i], null, right[i]));
103105
} else if(leftType === OBJECT) {
104106
result.push(new Diff(null, _getJsonsDiff(left[i], right[i]), NONE, OBJECT));
105-
} else {
107+
} else if(leftType === ARRAY){
106108
result.push(new Diff(null, _getArraysDiff(left[i], right[i]), NONE, ARRAY));
109+
} else {
110+
result.push(new Diff(null, null, NONE, NULL));
107111
}
108112
} else {
109113
result.push(new Diff(null, _getInDepthDiff(left[i], ADD), ADD, leftType));
@@ -134,8 +138,10 @@ function getDiffRepresentation(left, right) {
134138
result = result.concat(_getScalarsDiff(key, left[key], key, right[key]));
135139
} else if (leftType === OBJECT) {
136140
result.push(new Diff(key, _getJsonsDiff(left[key], right[key]), NONE, OBJECT));
137-
} else {
141+
} else if(leftType == ARRAY){
138142
result.push(new Diff(key, _getArraysDiff(left[key], right[key]), NONE, ARRAY));
143+
} else {
144+
result.push(new Diff(key, null, NONE, NULL));
139145
}
140146
} else {
141147
result.push(new Diff(key, _getInDepthDiff(left[key], ADD), ADD, leftType));

test/spec/json-diff-spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,28 @@ describe("Get Json diff representation", function() {
222222
};
223223
expect(call).toThrow(new ValidationException("Input is not a valid JSON", null));
224224
});
225+
226+
it("Diff should throw exception with correct error message when one of jsons is null", function() {
227+
var call = function() {
228+
getDiffRepresentation("{}", "null");
229+
};
230+
expect(call).toThrow(new ValidationException(null, "Input is not a valid JSON"));
231+
});
232+
233+
it("Diff should handle null values", function() {
234+
var result = getDiffRepresentation("{\"a\":null,\"b\":[null]}", "{\"b\":[null],\"a\":null}");
235+
236+
expect(result.topType).toEqual(OBJECT);
237+
expect(result.diff[0].key).toEqual("a");
238+
expect(result.diff[0].op).toEqual(NONE);
239+
expect(result.diff[0].valueType).toEqual(NULL);
240+
expect(result.diff[0].value).toEqual(null);
241+
expect(result.diff[1].key).toEqual("b");
242+
expect(result.diff[1].op).toEqual(NONE);
243+
expect(result.diff[1].valueType).toEqual(ARRAY);
244+
expect(result.diff[1].value[0].key).toEqual(null);
245+
expect(result.diff[1].value[0].value).toEqual(null);
246+
expect(result.diff[1].value[0].valueType).toEqual(NULL);
247+
expect(result.diff[1].value[0].op).toEqual(NONE);
248+
});
225249
});

0 commit comments

Comments
 (0)