src/jdk/nashorn/internal/objects/NativeJSON.java

Print this page




 145                     item = (String) v;
 146                 } else if (v instanceof ConsString) {
 147                     item = v.toString();
 148                 } else if (v instanceof Number ||
 149                         v instanceof NativeNumber ||
 150                         v instanceof NativeString) {
 151                     item = JSType.toString(v);
 152                 }
 153 
 154                 if (item != null) {
 155                     state.propertyList.add(item);
 156                 }
 157             }
 158         }
 159 
 160         // If the space parameter is a number, make an indent
 161         // string containing that many spaces.
 162 
 163         String gap;
 164 
 165         if (space instanceof Number || space instanceof NativeNumber) {
 166             int indent;
 167             if (space instanceof NativeNumber) {
 168                 indent = ((NativeNumber)space).intValue();
 169             } else {
 170                 indent = ((Number)space).intValue();
 171             }
 172 





 173             final StringBuilder sb = new StringBuilder();
 174             for (int i = 0; i < Math.min(10, indent); i++) {
 175                 sb.append(' ');
 176             }
 177             gap = sb.toString();
 178 
 179         } else if (space instanceof String || space instanceof ConsString || space instanceof NativeString) {
 180             final String str = (space instanceof String) ? (String)space : space.toString();
 181             gap = str.substring(0, Math.min(10, str.length()));
 182         } else {
 183             gap = "";
 184         }
 185 
 186         state.gap = gap;
 187 
 188         final ScriptObject wrapper = Global.newEmptyInstance();
 189         wrapper.set("", value, false);
 190 
 191         return str("", wrapper, state);
 192     }
 193 
 194     // -- Internals only below this point
 195 
 196     // stringify helpers.
 197 
 198     private static class StringifyState {
 199         final Map<ScriptObject, ScriptObject> stack = new IdentityHashMap<>();
 200 




 145                     item = (String) v;
 146                 } else if (v instanceof ConsString) {
 147                     item = v.toString();
 148                 } else if (v instanceof Number ||
 149                         v instanceof NativeNumber ||
 150                         v instanceof NativeString) {
 151                     item = JSType.toString(v);
 152                 }
 153 
 154                 if (item != null) {
 155                     state.propertyList.add(item);
 156                 }
 157             }
 158         }
 159 
 160         // If the space parameter is a number, make an indent
 161         // string containing that many spaces.
 162 
 163         String gap;
 164 
 165         // modifiable 'space' - parameter is final
 166         Object modSpace = space;
 167         if (modSpace instanceof NativeNumber) {
 168             modSpace = JSType.toNumber(JSType.toPrimitive(modSpace, Number.class));
 169         } else if (modSpace instanceof NativeString) {
 170             modSpace = JSType.toString(JSType.toPrimitive(modSpace, String.class));
 171         }
 172 
 173         if (modSpace instanceof Number) {
 174             int indent = Math.min(10, JSType.toInteger(modSpace));
 175             if (indent < 1) {
 176                 gap = "";
 177             } else {
 178                 final StringBuilder sb = new StringBuilder();
 179                 for (int i = 0; i < indent; i++) {
 180                     sb.append(' ');
 181                 }
 182                 gap = sb.toString();
 183             }
 184         } else if (modSpace instanceof String || modSpace instanceof ConsString) {
 185             final String str = modSpace.toString();
 186             gap = str.substring(0, Math.min(10, str.length()));
 187         } else {
 188             gap = "";
 189         }
 190 
 191         state.gap = gap;
 192 
 193         final ScriptObject wrapper = Global.newEmptyInstance();
 194         wrapper.set("", value, false);
 195 
 196         return str("", wrapper, state);
 197     }
 198 
 199     // -- Internals only below this point
 200 
 201     // stringify helpers.
 202 
 203     private static class StringifyState {
 204         final Map<ScriptObject, ScriptObject> stack = new IdentityHashMap<>();
 205