Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility function equivalent to angular.fromJson #8014

Merged
merged 3 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ angular.module('umbraco.mocks').
name: "1 column layout",
sections: [
{
grid: 12,
grid: 12
}
]
},
{
name: "2 column layout",
sections: [
{
grid: 4,
grid: 4
},
{
grid: 8
Expand Down Expand Up @@ -139,7 +139,7 @@ angular.module('umbraco.mocks').
}

}
},
}
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 12 additions & 1 deletion src/Umbraco.Web.UI.Client/src/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -106,10 +116,11 @@
isString: isString,
isNumber: isNumber,
isObject: isObject,
fromJson: fromJson,
toJson: toJson
};

if (typeof (window.Utilities) === 'undefined') {
window.Utilities = _utilities;
}
})(window);
})(window);
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/Umbraco.Web.UI.Client/test/unit/utilities.spec.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down