MCPcopy
hub / github.com/facebook/react / resolveToJSON

Function resolveToJSON

packages/react-client/src/ReactFlightReplyClient.js:357–859  ·  view source on GitHub ↗
(
    this:
      | {+[key: string | number]: ReactServerValue}
      | $ReadOnlyArray<ReactServerValue>,
    key: string,
    value: ReactServerValue,
  )

Source from the content-addressed store, hash-verified

355 }
356
357 function resolveToJSON(
358 this:
359 | {+[key: string | number]: ReactServerValue}
360 | $ReadOnlyArray<ReactServerValue>,
361 key: string,
362 value: ReactServerValue,
363 ): ReactJSONValue {
364 const parent = this;
365
366 if (__DEV__) {
367 if (key === __PROTO__) {
368 console.error(
369 'Expected not to serialize an object with own property `__proto__`. When parsed this property will be omitted.%s',
370 describeObjectForErrorMessage(parent, key),
371 );
372 }
373 }
374
375 // Make sure that `parent[key]` wasn't JSONified before `value` was passed to us
376 if (__DEV__) {
377 // $FlowFixMe[incompatible-use]
378 const originalValue = parent[key];
379 if (
380 typeof originalValue === 'object' &&
381 originalValue !== value &&
382 !(originalValue instanceof Date)
383 ) {
384 if (objectName(originalValue) !== 'Object') {
385 console.error(
386 'Only plain objects can be passed to Server Functions from the Client. ' +
387 '%s objects are not supported.%s',
388 objectName(originalValue),
389 describeObjectForErrorMessage(parent, key),
390 );
391 } else {
392 console.error(
393 'Only plain objects can be passed to Server Functions from the Client. ' +
394 'Objects with toJSON methods are not supported. Convert it manually ' +
395 'to a simple value before passing it to props.%s',
396 describeObjectForErrorMessage(parent, key),
397 );
398 }
399 }
400 }
401
402 if (value === null) {
403 return null;
404 }
405
406 if (typeof value === 'object') {
407 switch ((value: any).$$typeof) {
408 case REACT_ELEMENT_TYPE: {
409 if (temporaryReferences !== undefined && key.indexOf(':') === -1) {
410 // TODO: If the property name contains a colon, we don't dedupe. Escape instead.
411 const parentReference = writtenObjects.get(parent);
412 if (parentReference !== undefined) {
413 // If the parent has a reference, we can refer to this object indirectly
414 // through the property name inside that parent.

Callers

nothing calls this directly

Calls 15

objectNameFunction · 0.90
writeTemporaryReferenceFunction · 0.90
isSimpleObjectFunction · 0.90
serializeModelFunction · 0.85
isArrayFunction · 0.85
serializeMapIDFunction · 0.85
serializeSetIDFunction · 0.85
serializeBlobIDFunction · 0.85
serializeIteratorIDFunction · 0.85

Tested by

no test coverage detected