|
| 1 | +note |
| 2 | + description: "JSON_PRETTY_STRING_VISITOR Generates the JSON-String for a JSON_VALUE" |
| 3 | + revision: "0.1" |
| 4 | + |
| 5 | +class |
| 6 | + JSON_PRETTY_STRING_VISITOR |
| 7 | + |
| 8 | +inherit |
| 9 | + JSON_VISITOR |
| 10 | + |
| 11 | +create |
| 12 | + make, |
| 13 | + make_custom |
| 14 | + |
| 15 | +feature -- Initialization |
| 16 | + |
| 17 | + make (a_output: like output) |
| 18 | + -- Create a new instance |
| 19 | + do |
| 20 | + make_custom (a_output, 1, 1) |
| 21 | + end |
| 22 | + |
| 23 | + make_custom (a_output: like output; a_object_count_inlining, a_array_count_inlining: INTEGER) |
| 24 | + -- Create a new instance |
| 25 | + do |
| 26 | + output := a_output |
| 27 | + create indentation.make_empty |
| 28 | + indentation_step := "%T" |
| 29 | + |
| 30 | + object_count_inlining := a_object_count_inlining |
| 31 | + array_count_inlining := a_array_count_inlining |
| 32 | + end |
| 33 | + |
| 34 | +feature -- Access |
| 35 | + |
| 36 | + output: STRING_32 |
| 37 | + -- JSON representation |
| 38 | + |
| 39 | + indentation: like output |
| 40 | + |
| 41 | + indentation_step: like indentation |
| 42 | + |
| 43 | + line_number: INTEGER |
| 44 | + |
| 45 | + indent |
| 46 | + do |
| 47 | + indentation.append (indentation_step) |
| 48 | + end |
| 49 | + |
| 50 | + exdent |
| 51 | + do |
| 52 | + indentation.remove_tail (indentation_step.count) |
| 53 | + end |
| 54 | + |
| 55 | + new_line |
| 56 | + do |
| 57 | + output.append ("%N") |
| 58 | + output.append (indentation) |
| 59 | + line_number := line_number + 1 |
| 60 | + end |
| 61 | + |
| 62 | + object_count_inlining: INTEGER |
| 63 | + array_count_inlining: INTEGER |
| 64 | + |
| 65 | +feature -- Visitor Pattern |
| 66 | + |
| 67 | + visit_json_array (a_json_array: JSON_ARRAY) |
| 68 | + -- Visit `a_json_array'. |
| 69 | + local |
| 70 | + value: JSON_VALUE |
| 71 | + l_json_array: ARRAYED_LIST [JSON_VALUE] |
| 72 | + l_line: like line_number |
| 73 | + l_multiple_lines: BOOLEAN |
| 74 | + do |
| 75 | + l_json_array := a_json_array.array_representation |
| 76 | + l_multiple_lines := l_json_array.count >= array_count_inlining or across l_json_array as p some attached {JSON_OBJECT} p.item or attached {JSON_ARRAY} p.item end |
| 77 | + output.append ("[") |
| 78 | + l_line := line_number |
| 79 | + indent |
| 80 | + from |
| 81 | + l_json_array.start |
| 82 | + until |
| 83 | + l_json_array.off |
| 84 | + loop |
| 85 | + if |
| 86 | + line_number > l_line or |
| 87 | + l_multiple_lines |
| 88 | + then |
| 89 | + new_line |
| 90 | + end |
| 91 | + value := l_json_array.item |
| 92 | + value.accept (Current) |
| 93 | + l_json_array.forth |
| 94 | + if not l_json_array.after then |
| 95 | + output.append (", ") |
| 96 | + end |
| 97 | + end |
| 98 | + exdent |
| 99 | + if |
| 100 | + line_number > l_line or |
| 101 | + l_json_array.count >= array_count_inlining |
| 102 | + then |
| 103 | + new_line |
| 104 | + end |
| 105 | + output.append ("]") |
| 106 | + end |
| 107 | + |
| 108 | + visit_json_boolean (a_json_boolean: JSON_BOOLEAN) |
| 109 | + -- Visit `a_json_boolean'. |
| 110 | + do |
| 111 | + output.append (a_json_boolean.item.out) |
| 112 | + end |
| 113 | + |
| 114 | + visit_json_null (a_json_null: JSON_NULL) |
| 115 | + -- Visit `a_json_null'. |
| 116 | + do |
| 117 | + output.append ("null") |
| 118 | + end |
| 119 | + |
| 120 | + visit_json_number (a_json_number: JSON_NUMBER) |
| 121 | + -- Visit `a_json_number'. |
| 122 | + do |
| 123 | + output.append (a_json_number.item) |
| 124 | + end |
| 125 | + |
| 126 | + visit_json_object (a_json_object: JSON_OBJECT) |
| 127 | + -- Visit `a_json_object'. |
| 128 | + local |
| 129 | + l_pairs: HASH_TABLE [JSON_VALUE, JSON_STRING] |
| 130 | + l_line: like line_number |
| 131 | + l_multiple_lines: BOOLEAN |
| 132 | + do |
| 133 | + l_pairs := a_json_object.map_representation |
| 134 | + l_multiple_lines := l_pairs.count >= object_count_inlining or across l_pairs as p some attached {JSON_OBJECT} p.item or attached {JSON_ARRAY} p.item end |
| 135 | + output.append ("{") |
| 136 | + l_line := line_number |
| 137 | + indent |
| 138 | + from |
| 139 | + l_pairs.start |
| 140 | + until |
| 141 | + l_pairs.off |
| 142 | + loop |
| 143 | + if |
| 144 | + line_number > l_line or |
| 145 | + l_multiple_lines |
| 146 | + then |
| 147 | + new_line |
| 148 | + end |
| 149 | + l_pairs.key_for_iteration.accept (Current) |
| 150 | + output.append (": ") |
| 151 | + l_pairs.item_for_iteration.accept (Current) |
| 152 | + l_pairs.forth |
| 153 | + if not l_pairs.after then |
| 154 | + output.append (", ") |
| 155 | + end |
| 156 | + end |
| 157 | + exdent |
| 158 | + if |
| 159 | + line_number > l_line or |
| 160 | + l_pairs.count >= object_count_inlining |
| 161 | + then |
| 162 | + new_line |
| 163 | + end |
| 164 | + output.append ("}") |
| 165 | + end |
| 166 | + |
| 167 | + visit_json_string (a_json_string: JSON_STRING) |
| 168 | + -- Visit `a_json_string'. |
| 169 | + do |
| 170 | + output.append ("%"") |
| 171 | + output.append (a_json_string.item) |
| 172 | + output.append ("%"") |
| 173 | + end |
| 174 | + |
| 175 | +end |
0 commit comments