Merge pull request #2798 from openMF/revert-2747-fix/unsafe-json-parse
Revert "WEB-(394)-fix: replace all direct JSON.parse usages with safe parsing utilities"
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
export function safeParse<T>(raw: string | null | undefined, defaultValue: T): T {
|
||||
if (!raw) return defaultValue;
|
||||
try {
|
||||
return JSON.parse(raw) as T;
|
||||
} catch {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
export function safeParseObject<T extends object>(raw: string | null | undefined, defaultValue: T): T {
|
||||
if (!raw) return defaultValue;
|
||||
try {
|
||||
const parsed = JSON.parse(raw);
|
||||
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? (parsed as T) : defaultValue;
|
||||
} catch {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
export function safeParseArray<T = unknown>(raw: string | null | undefined, defaultValue: T[] = []): T[] {
|
||||
if (!raw) return defaultValue;
|
||||
try {
|
||||
const parsed = JSON.parse(raw);
|
||||
return Array.isArray(parsed) ? (parsed as T[]) : defaultValue;
|
||||
} catch {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { AlertService } from 'app/core/alert/alert.service';
|
||||
import { Dates } from 'app/core/utils/dates';
|
||||
import { safeParse, safeParseArray, safeParseObject } from 'app/core/utils/json';
|
||||
|
||||
/** Environment Imports */
|
||||
import { environment } from '../../environments/environment';
|
||||
@@ -109,9 +108,8 @@ export class SettingsService {
|
||||
/**
|
||||
* Returns date format setting.
|
||||
*/
|
||||
get dateFormat(): string {
|
||||
const parsed = safeParse<string | null>(localStorage.getItem('mifosXDateFormat'), null);
|
||||
return typeof parsed === 'string' && parsed.length > 0 ? parsed : 'dd MMMM yyyy';
|
||||
get dateFormat() {
|
||||
return JSON.parse(localStorage.getItem('mifosXDateFormat'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -121,10 +119,7 @@ export class SettingsService {
|
||||
if (!localStorage.getItem('mifosXLanguage')) {
|
||||
this.setDefaultLanguage();
|
||||
}
|
||||
return safeParseObject<{ name: string; code: string } | undefined>(
|
||||
localStorage.getItem('mifosXLanguage'),
|
||||
undefined
|
||||
);
|
||||
return JSON.parse(localStorage.getItem('mifosXLanguage'));
|
||||
}
|
||||
|
||||
get languageCode() {
|
||||
@@ -152,7 +147,7 @@ export class SettingsService {
|
||||
* Returns list of default server
|
||||
*/
|
||||
get servers() {
|
||||
return safeParseArray<string>(localStorage.getItem('mifosXServers'), []);
|
||||
return JSON.parse(localStorage.getItem('mifosXServers'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -165,7 +160,7 @@ export class SettingsService {
|
||||
if (environment.baseApiUrl && environment.baseApiUrl !== '') {
|
||||
return environment.baseApiUrl;
|
||||
} else {
|
||||
return this.servers[0];
|
||||
return this.servers()[0];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,7 +217,7 @@ export class SettingsService {
|
||||
* Returns list of Tenant Identifiers
|
||||
*/
|
||||
get tenantIdentifiers(): any {
|
||||
return safeParseArray<string>(localStorage.getItem('mifosXTenantIdentifiers'), []);
|
||||
return JSON.parse(localStorage.getItem('mifosXTenantIdentifiers'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,6 +260,6 @@ export class SettingsService {
|
||||
}
|
||||
|
||||
get themeDarkEnabled(): boolean {
|
||||
return safeParse<boolean>(localStorage.getItem('mifosXThemeDarkEnabled'), false);
|
||||
return JSON.parse(localStorage.getItem('mifosXThemeDarkEnabled'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Injectable, EventEmitter } from '@angular/core';
|
||||
import { safeParseObject } from 'app/core/utils/json';
|
||||
import { Theme } from './theme.model';
|
||||
import { ThemeManagerService } from './theme-manager.service';
|
||||
|
||||
@@ -20,7 +19,7 @@ export class ThemeStorageService {
|
||||
}
|
||||
|
||||
getTheme(): Theme {
|
||||
return safeParseObject<Theme | null>(localStorage.getItem(this.themeStorageKey), null);
|
||||
return JSON.parse(localStorage.getItem(this.themeStorageKey));
|
||||
}
|
||||
|
||||
clearTheme() {
|
||||
|
||||
@@ -46,7 +46,7 @@ export class BusinessDateTabComponent implements OnInit {
|
||||
businessDateData: any;
|
||||
|
||||
dateIndex = 0;
|
||||
userDateFormat: string = '';
|
||||
userDateFormat: '';
|
||||
isBusinessDateEnabled = false;
|
||||
isEditInProgress = false;
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@ import { MatDivider } from '@angular/material/divider';
|
||||
import { NgIf, NgFor, KeyValuePipe } from '@angular/common';
|
||||
import { DateFormatPipe } from '../../pipes/date-format.pipe';
|
||||
import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module';
|
||||
import { safeParseObject } from 'app/core/utils/json';
|
||||
|
||||
@Component({
|
||||
selector: 'mifosx-view-checker-inbox',
|
||||
@@ -53,7 +52,7 @@ export class ViewCheckerInboxComponent {
|
||||
) {
|
||||
this.route.data.subscribe((data: { checkerInboxDetail: any }) => {
|
||||
this.checkerInboxDetail = data.checkerInboxDetail;
|
||||
this.jsondata = safeParseObject<any>(this.checkerInboxDetail.commandAsJson, {});
|
||||
this.jsondata = JSON.parse(this.checkerInboxDetail.commandAsJson);
|
||||
this.displayJSONData = !_.isEmpty(this.jsondata);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -31,7 +31,6 @@ import { Dates } from './core/utils/dates';
|
||||
import { animate, style, transition, trigger } from '@angular/animations';
|
||||
import { I18nService } from './core/i18n/i18n.service';
|
||||
import { ThemingService } from './shared/theme-toggle/theming.service';
|
||||
import { safeParseArray } from 'app/core/utils/json';
|
||||
|
||||
import { AuthService } from './zitadel/auth.service';
|
||||
|
||||
@@ -196,7 +195,7 @@ export class WebAppComponent implements OnInit, OnDestroy {
|
||||
// Stores top 100 user activites as local storage object.
|
||||
let activities: string[] = [];
|
||||
if (localStorage.getItem('mifosXLocation')) {
|
||||
const activitiesArray = safeParseArray<string>(localStorage.getItem('mifosXLocation'), []);
|
||||
const activitiesArray: string[] = JSON.parse(localStorage.getItem('mifosXLocation'));
|
||||
const length = activitiesArray.length;
|
||||
activities = length > 100 ? activitiesArray.slice(length - 100) : activitiesArray;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user