( serializable: gdSerializable, methodName: string = 'serializeTo', options: ?SerializationOptions = undefined )
| 52 | * @param {*} options Optional serialization options (e.g. canonical mode) |
| 53 | */ |
| 54 | export function serializeToJSObject( |
| 55 | serializable: gdSerializable, |
| 56 | methodName: string = 'serializeTo', |
| 57 | options: ?SerializationOptions = undefined |
| 58 | ): any { |
| 59 | return withSerializationOptions(options, () => { |
| 60 | const serializedElement = new gd.SerializerElement(); |
| 61 | serializable[methodName](serializedElement); |
| 62 | |
| 63 | // JSON.parse + toJSON is 30% faster than gd.Serializer.toJSObject. |
| 64 | const json = gd.Serializer.toJSON(serializedElement); |
| 65 | |
| 66 | try { |
| 67 | const object = JSON.parse(json); |
| 68 | |
| 69 | serializedElement.delete(); |
| 70 | return object; |
| 71 | } catch (error) { |
| 72 | serializedElement.delete(); |
| 73 | console.error( |
| 74 | 'Invalid JSON when serializing to JS object. toJSON should always return a valid JSON string.', |
| 75 | { json, error } |
| 76 | ); |
| 77 | throw error; |
| 78 | } |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | export function serializeObjectWithCleanDefaultBehaviorFlags( |
| 83 | object: gdObject |
no test coverage detected