Add utility function equivalent to angular.fromJson (#8014)
This commit is contained in:
committed by
GitHub
parent
fff23095a6
commit
88c842ccda
+2
-2
@@ -39,7 +39,7 @@ angular.module("umbraco.directives")
|
||||
|
||||
function stringToJson(text) {
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
return Utilities.fromJson(text);
|
||||
} catch (err) {
|
||||
setInvalid();
|
||||
return text;
|
||||
@@ -55,7 +55,7 @@ angular.module("umbraco.directives")
|
||||
function isValidJson(model) {
|
||||
var flag = true;
|
||||
try {
|
||||
JSON.parse(model)
|
||||
Utilities.fromJson(model)
|
||||
} catch (err) {
|
||||
flag = false;
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ angular.module('umbraco.mocks').
|
||||
name: "1 column layout",
|
||||
sections: [
|
||||
{
|
||||
grid: 12,
|
||||
grid: 12
|
||||
}
|
||||
]
|
||||
},
|
||||
@@ -98,7 +98,7 @@ angular.module('umbraco.mocks').
|
||||
name: "2 column layout",
|
||||
sections: [
|
||||
{
|
||||
grid: 4,
|
||||
grid: 4
|
||||
},
|
||||
{
|
||||
grid: 8
|
||||
@@ -139,7 +139,7 @@ angular.module('umbraco.mocks').
|
||||
}
|
||||
|
||||
}
|
||||
},
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -206,7 +206,7 @@ function umbSessionStorage($window) {
|
||||
return {
|
||||
|
||||
get: function (key) {
|
||||
return JSON.parse(storage["umb_" + key]);
|
||||
return Utilities.fromJson(storage["umb_" + key]);
|
||||
},
|
||||
|
||||
set: function (key, value) {
|
||||
|
||||
@@ -94,6 +94,16 @@
|
||||
return JSON.stringify(obj, toJsonReplacer, pretty);
|
||||
}
|
||||
|
||||
/**
|
||||
* Equivalent to angular.fromJson
|
||||
*/
|
||||
const fromJson = (val) => {
|
||||
if (!isString(val)) {
|
||||
return val;
|
||||
}
|
||||
return JSON.parse(val);
|
||||
}
|
||||
|
||||
let _utilities = {
|
||||
noop: noop,
|
||||
copy: copy,
|
||||
@@ -106,10 +116,11 @@
|
||||
isString: isString,
|
||||
isNumber: isNumber,
|
||||
isObject: isObject,
|
||||
fromJson: fromJson,
|
||||
toJson: toJson
|
||||
};
|
||||
|
||||
if (typeof (window.Utilities) === 'undefined') {
|
||||
window.Utilities = _utilities;
|
||||
}
|
||||
})(window);
|
||||
})(window);
|
||||
|
||||
+2
-2
@@ -70,8 +70,8 @@ function MacroPickerController($scope, entityResource, macroResource, umbPropEdi
|
||||
//detect if it is a json string
|
||||
if (val.detectIsJson()) {
|
||||
try {
|
||||
//Parse it to json
|
||||
prop.value = JSON.parse(val);
|
||||
//Parse it from json
|
||||
prop.value = Utilities.fromJson(val);
|
||||
}
|
||||
catch (e) {
|
||||
// not json
|
||||
|
||||
+1
-1
@@ -189,7 +189,7 @@ angular.module("umbraco").controller("Umbraco.Editors.TreePickerController",
|
||||
if ($scope.model.filter.startsWith("{")) {
|
||||
$scope.model.filterAdvanced = true;
|
||||
//convert to object
|
||||
$scope.model.filter = JSON.parse($scope.model.filter);
|
||||
$scope.model.filter = Utilities.fromJson($scope.model.filter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
(function () {
|
||||
describe("Utilities", function () {
|
||||
describe("fromJson", function () {
|
||||
it("should deserialize json as object", function () {
|
||||
expect(Utilities.fromJson('{"a":1,"b":2}')).toEqual({ a: 1, b: 2 });
|
||||
});
|
||||
it("should return object as object", function () {
|
||||
expect(Utilities.fromJson({ a: 1, b: 2 })).toEqual({ a: 1, b: 2 });
|
||||
});
|
||||
}),
|
||||
describe("toJson", function () {
|
||||
it("should delegate to JSON.stringify", function () {
|
||||
var spy = spyOn(JSON, "stringify").and.callThrough();
|
||||
|
||||
Reference in New Issue
Block a user