1- // ignore_for_file: avoid_print
1+ // ignore_for_file: avoid_print, prefer_foreach
22
33import 'dart:convert' ;
44import 'dart:io' ;
55
6- T ? removeBlankEntries < T >( T ? json) {
6+ dynamic removeBlankEntries ( dynamic json) {
77 // This function is protected by BSD 3-Clause License
88 // Changes made to this section are allow removing of '' values from JSON
99
@@ -37,23 +37,47 @@ T? removeBlankEntries<T>(T? json) {
3737 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3838 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3939 */
40+
4041 if (json == null ) {
4142 return null ;
4243 }
44+
4345 if (json is List ) {
44- json.removeWhere ((e) => e == null );
45- json.forEach (removeBlankEntries);
46- // If the list is empty after removing nulls, return null to remove it.
46+ for (int i = json.length - 1 ; i >= 0 ; i-- ) {
47+ final processedElement = removeBlankEntries (json[i]);
48+ if (processedElement == null ) {
49+ json.removeAt (i);
50+ } else {
51+ json[i] = processedElement;
52+ }
53+ }
4754 return json.isEmpty ? null : json;
4855 } else if (json is Map ) {
49- json.removeWhere (
50- (key, value) => key == null || value == null || value == '' ,
51- );
52- json.values.forEach (removeBlankEntries);
53- // If the map is empty after removing blank entries, return null to remove it.
56+ final keysToRemove = < dynamic > {};
57+ final keys = json.keys.toList ();
58+
59+ for (final key in keys) {
60+ if (key == null ) {
61+ keysToRemove.add (key);
62+ continue ;
63+ }
64+ final processedValue = removeBlankEntries (json[key]);
65+ if (processedValue == null || processedValue == '' ) {
66+ keysToRemove.add (key);
67+ } else {
68+ json[key] = processedValue;
69+ }
70+ }
71+ for (final key in keysToRemove) {
72+ json.remove (key);
73+ }
5474 return json.isEmpty ? null : json;
75+ }
5576
77+ if (json is String && json.isEmpty) {
78+ return null ;
5679 }
80+
5781 return json;
5882}
5983
@@ -62,33 +86,44 @@ Future<void> processJsonFiles() async {
6286 final List <FileSystemEntity > files = directory.listSync ();
6387
6488 for (final file in files) {
89+ if (! file.path.endsWith ('.json' ) || file is ! File ) {
90+ continue ;
91+ }
6592 try {
66- if (file is File && file.path.endsWith ('.json' )) {
67- final String contents = await file.readAsString ();
68- final dynamic json = jsonDecode (contents);
69- final dynamic processedJson = removeBlankEntries (json);
70- bool isEmpty = false ;
71-
72- if (processedJson is Map ) {
73- isEmpty = processedJson.values.every ((value) => value is Map && value.isEmpty);
74- }
75-
76- if (processedJson == null || isEmpty) {
77- await file.delete ();
78- print ('🗑️ File deleted: ${file .path }' );
79- } else {
80- await file.writeAsString (
81- const JsonEncoder .withIndent (' ' ).convert (processedJson),
82- );
83- print ('🥞 Task successful on: ${file .path }' );
84- }
93+ final contents = await file.readAsString ();
94+ if (contents.trim ().isEmpty) {
95+ print ('🗑️ File is empty, deleting: ${file .path }' );
96+ await file.delete ();
97+ continue ;
98+ }
99+
100+ dynamic jsonInput;
101+ try {
102+ jsonInput = jsonDecode (contents);
103+ } on FormatException catch (e, stackTrace) {
104+ print ('💥 Invalid JSON in file: ${file .path }: $e ' );
105+ print (stackTrace);
106+ continue ;
107+ }
108+
109+ final dynamic processedJson = removeBlankEntries (jsonInput);
110+ if (processedJson == null ) {
111+ await file.delete ();
112+ print ('🗑️ File resulted in empty JSON, deleted: ${file .path }' );
113+ } else {
114+ final prettyJson = const JsonEncoder .withIndent (
115+ ' ' , // Two spaces
116+ ).convert (processedJson);
117+ await file.writeAsString (prettyJson);
118+ print ('🥞 Task successful on: ${file .path }' );
85119 }
86- } catch (e) {
120+ } catch (e, stackTrace ) {
87121 print ('💥 Task failed on: ${file .path }: $e ' );
122+ print (stackTrace);
88123 }
89124 }
90125}
91126
92127void main () async {
93- processJsonFiles ();
128+ await processJsonFiles ();
94129}
0 commit comments