MCPcopy Create free account
hub / github.com/mozilla/rhino / ja

Method ja

rhino/src/main/java/org/mozilla/javascript/NativeJSON.java:454–522  ·  view source on GitHub ↗
(Scriptable value, StringifyState state)

Source from the content-addressed store, hash-verified

452 }
453
454 private static String ja(Scriptable value, StringifyState state) {
455 Object trackValue = value, unwrapped = null;
456 if (value instanceof Wrapper) {
457 trackValue = unwrapped = ((Wrapper) value).unwrap();
458 }
459 if (state.stack.contains(trackValue)) {
460 throw ScriptRuntime.typeErrorById("msg.cyclic.value", trackValue.getClass().getName());
461 }
462 state.stack.push(trackValue);
463
464 String stepback = state.indent;
465 state.indent = state.indent + state.gap;
466 ArrayList<Object> partial = new ArrayList<>();
467
468 if (unwrapped != null) {
469 Object[] elements = null;
470 if (unwrapped.getClass().isArray()) {
471 int length = Array.getLength(unwrapped);
472 elements = new Object[length];
473 for (int i = 0; i < length; i++) {
474 elements[i] = Context.javaToJS(Array.get(unwrapped, i), state.scope, state.cx);
475 }
476 } else if (unwrapped instanceof Collection) {
477 Collection<?> collection = (Collection<?>) unwrapped;
478 elements = new Object[collection.size()];
479 int i = 0;
480 for (Object o : collection) {
481 elements[i++] = Context.javaToJS(o, state.scope, state.cx);
482 }
483 }
484 if (elements != null) {
485 value = state.cx.newArray(state.scope, elements);
486 }
487 }
488
489 long len = ((NativeArray) value).getLength();
490
491 for (long index = 0; index < len; index++) {
492 Object strP;
493 if (index > Integer.MAX_VALUE) {
494 strP = str(Long.toString(index), value, state);
495 } else {
496 strP = str(Integer.valueOf((int) index), value, state);
497 }
498 if (strP == Undefined.instance) {
499 partial.add("null");
500 } else {
501 partial.add(strP);
502 }
503 }
504
505 final String finalValue;
506
507 if (partial.isEmpty()) {
508 finalValue = "[]";
509 } else {
510 if (state.gap.length() == 0) {
511 finalValue = '[' + join(partial, ",") + ']';

Callers 1

strMethod · 0.95

Calls 15

typeErrorByIdMethod · 0.95
javaToJSMethod · 0.95
strMethod · 0.95
joinMethod · 0.95
getClassMethod · 0.80
unwrapMethod · 0.65
getNameMethod · 0.65
isArrayMethod · 0.65
getLengthMethod · 0.65
getMethod · 0.65
sizeMethod · 0.65
newArrayMethod · 0.65

Tested by

no test coverage detected