Home / Function/ toJSON() — react Function Reference

toJSON() — react Function Reference

Architecture documentation for the toJSON() function in shared-runtime.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  3c44ef67_7a19_657e_bba6_9d9cff25667a["toJSON()"]
  9afdd6fb_14c3_0a52_8105_2d82e58c5c28["shared-runtime.ts"]
  3c44ef67_7a19_657e_bba6_9d9cff25667a -->|defined in| 9afdd6fb_14c3_0a52_8105_2d82e58c5c28
  b55af0df_175a_b10d_f381_c6dca3d1fd0f["WrapperTestComponent()"]
  b55af0df_175a_b10d_f381_c6dca3d1fd0f -->|calls| 3c44ef67_7a19_657e_bba6_9d9cff25667a
  c389538a_5c1f_5af6_98fc_d2b023fd4e74["Stringify()"]
  c389538a_5c1f_5af6_98fc_d2b023fd4e74 -->|calls| 3c44ef67_7a19_657e_bba6_9d9cff25667a
  style 3c44ef67_7a19_657e_bba6_9d9cff25667a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/snap/src/sprout/shared-runtime.ts lines 315–347

export function toJSON(value: any, invokeFns: boolean = false): string {
  const seen = new Map();

  return JSON.stringify(value, (_key: string, val: any) => {
    if (typeof val === 'function') {
      if (val.length === 0 && invokeFns) {
        return {
          kind: 'Function',
          result: val(),
        };
      } else {
        return `[[ function params=${val.length} ]]`;
      }
    } else if (typeof val === 'object') {
      let id = seen.get(val);
      if (id != null) {
        return `[[ cyclic ref *${id} ]]`;
      } else if (val instanceof Map) {
        return {
          kind: 'Map',
          value: Array.from(val.entries()),
        };
      } else if (val instanceof Set) {
        return {
          kind: 'Set',
          value: Array.from(val.values()),
        };
      }
      seen.set(val, seen.size);
    }
    return val;
  });
}

Domain

Subdomains

Frequently Asked Questions

What does toJSON() do?
toJSON() is a function in the react codebase, defined in compiler/packages/snap/src/sprout/shared-runtime.ts.
Where is toJSON() defined?
toJSON() is defined in compiler/packages/snap/src/sprout/shared-runtime.ts at line 315.
What calls toJSON()?
toJSON() is called by 2 function(s): Stringify, WrapperTestComponent.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free