JsonSerializer.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. using System.Globalization;
  2. using System.Runtime.CompilerServices;
  3. using System.Text;
  4. using Jint.Collections;
  5. using Jint.Native.BigInt;
  6. using Jint.Native.Boolean;
  7. using Jint.Native.Number;
  8. using Jint.Native.Object;
  9. using Jint.Native.String;
  10. using Jint.Runtime;
  11. using Jint.Runtime.Descriptors;
  12. using Jint.Runtime.Interop;
  13. namespace Jint.Native.Json;
  14. public sealed class JsonSerializer
  15. {
  16. private readonly Engine _engine;
  17. private ObjectTraverseStack _stack = null!;
  18. private string? _indent;
  19. private string _gap = string.Empty;
  20. private List<JsValue>? _propertyList;
  21. private JsValue _replacerFunction = JsValue.Undefined;
  22. private static readonly JsString toJsonProperty = new("toJSON");
  23. public JsonSerializer(Engine engine)
  24. {
  25. _engine = engine;
  26. }
  27. public JsValue Serialize(JsValue value)
  28. {
  29. return Serialize(value, JsValue.Undefined, JsValue.Undefined);
  30. }
  31. public JsValue Serialize(JsValue value, JsValue replacer, JsValue space)
  32. {
  33. _stack = new ObjectTraverseStack(_engine);
  34. // for JSON.stringify(), any function passed as the first argument will return undefined
  35. // if the replacer is not defined. The function is not called either.
  36. if (value.IsCallable && ReferenceEquals(replacer, JsValue.Undefined))
  37. {
  38. return JsValue.Undefined;
  39. }
  40. SetupReplacer(replacer);
  41. _gap = BuildSpacingGap(space);
  42. var wrapper = _engine.Realm.Intrinsics.Object.Construct(Arguments.Empty);
  43. wrapper.DefineOwnProperty(JsString.Empty, new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable));
  44. string result;
  45. var json = new ValueStringBuilder();
  46. try
  47. {
  48. if (SerializeJSONProperty(JsString.Empty, wrapper, ref json) == SerializeResult.Undefined)
  49. {
  50. return JsValue.Undefined;
  51. }
  52. }
  53. finally
  54. {
  55. result = json.ToString();
  56. }
  57. return new JsString(result);
  58. }
  59. private void SetupReplacer(JsValue replacer)
  60. {
  61. if (replacer is not ObjectInstance oi)
  62. {
  63. return;
  64. }
  65. if (oi.IsCallable)
  66. {
  67. _replacerFunction = replacer;
  68. }
  69. else
  70. {
  71. if (oi.IsArray())
  72. {
  73. _propertyList = new List<JsValue>();
  74. var len = oi.GetLength();
  75. var k = 0;
  76. while (k < len)
  77. {
  78. var prop = JsString.Create(k);
  79. var v = replacer.Get(prop);
  80. var item = JsValue.Undefined;
  81. if (v.IsString())
  82. {
  83. item = v;
  84. }
  85. else if (v.IsNumber())
  86. {
  87. item = TypeConverter.ToString(v);
  88. }
  89. else if (v.IsObject())
  90. {
  91. if (v is StringInstance or NumberInstance)
  92. {
  93. item = TypeConverter.ToString(v);
  94. }
  95. }
  96. if (!item.IsUndefined() && !_propertyList.Contains(item))
  97. {
  98. _propertyList.Add(item);
  99. }
  100. k++;
  101. }
  102. }
  103. }
  104. }
  105. private static string BuildSpacingGap(JsValue space)
  106. {
  107. if (space.IsObject())
  108. {
  109. var spaceObj = space.AsObject();
  110. if (spaceObj.Class == ObjectClass.Number)
  111. {
  112. space = TypeConverter.ToNumber(spaceObj);
  113. }
  114. else if (spaceObj.Class == ObjectClass.String)
  115. {
  116. space = TypeConverter.ToJsString(spaceObj);
  117. }
  118. }
  119. // defining the gap
  120. if (space.IsNumber())
  121. {
  122. var number = ((JsNumber) space)._value;
  123. if (number > 0)
  124. {
  125. return new string(' ', (int) System.Math.Min(10, number));
  126. }
  127. return string.Empty;
  128. }
  129. if (space.IsString())
  130. {
  131. var stringSpace = space.ToString();
  132. return stringSpace.Length <= 10 ? stringSpace : stringSpace.Substring(0, 10);
  133. }
  134. return string.Empty;
  135. }
  136. /// <summary>
  137. /// https://tc39.es/ecma262/#sec-serializejsonproperty
  138. /// </summary>
  139. private SerializeResult SerializeJSONProperty(JsValue key, JsValue holder, ref ValueStringBuilder json)
  140. {
  141. var value = ReadUnwrappedValue(key, holder);
  142. if (ReferenceEquals(value, JsValue.Null))
  143. {
  144. json.Append("null");
  145. return SerializeResult.NotUndefined;
  146. }
  147. if (value.IsBoolean())
  148. {
  149. json.Append(((JsBoolean) value)._value ? "true" : "false");
  150. return SerializeResult.NotUndefined;
  151. }
  152. if (value.IsString())
  153. {
  154. QuoteJSONString(value.ToString(), ref json);
  155. return SerializeResult.NotUndefined;
  156. }
  157. if (value.IsNumber())
  158. {
  159. var doubleValue = ((JsNumber) value)._value;
  160. if (value.IsInteger())
  161. {
  162. json.Append((long) doubleValue);
  163. return SerializeResult.NotUndefined;
  164. }
  165. var isFinite = !double.IsNaN(doubleValue) && !double.IsInfinity(doubleValue);
  166. if (isFinite)
  167. {
  168. if (TypeConverter.CanBeStringifiedAsLong(doubleValue))
  169. {
  170. json.Append((long) doubleValue);
  171. return SerializeResult.NotUndefined;
  172. }
  173. json.Append(NumberPrototype.ToNumberString(doubleValue));
  174. return SerializeResult.NotUndefined;
  175. }
  176. json.Append("null");
  177. return SerializeResult.NotUndefined;
  178. }
  179. if (value.IsBigInt())
  180. {
  181. ExceptionHelper.ThrowTypeError(_engine.Realm, "Do not know how to serialize a BigInt");
  182. }
  183. if (value is ObjectInstance { IsCallable: false } objectInstance)
  184. {
  185. if (CanSerializesAsArray(objectInstance))
  186. {
  187. SerializeJSONArray(objectInstance, ref json);
  188. return SerializeResult.NotUndefined;
  189. }
  190. if (objectInstance is IObjectWrapper wrapper
  191. && _engine.Options.Interop.SerializeToJson is { } serialize)
  192. {
  193. json.Append(serialize(wrapper.Target));
  194. return SerializeResult.NotUndefined;
  195. }
  196. SerializeJSONObject(objectInstance, ref json);
  197. return SerializeResult.NotUndefined;
  198. }
  199. return SerializeResult.Undefined;
  200. }
  201. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  202. private JsValue ReadUnwrappedValue(JsValue key, JsValue holder)
  203. {
  204. var value = holder.Get(key);
  205. if (value._type <= InternalTypes.Integer && _replacerFunction.IsUndefined())
  206. {
  207. return value;
  208. }
  209. var isBigInt = value is BigIntInstance || value.IsBigInt();
  210. if (value.IsObject() || isBigInt)
  211. {
  212. var toJson = value.GetV(_engine.Realm, toJsonProperty);
  213. if (toJson.IsUndefined() && isBigInt)
  214. {
  215. toJson = _engine.Realm.Intrinsics.BigInt.PrototypeObject.Get(toJsonProperty);
  216. }
  217. if (toJson.IsObject())
  218. {
  219. if (toJson.AsObject() is ICallable callableToJson)
  220. {
  221. value = callableToJson.Call(value, TypeConverter.ToPropertyKey(key));
  222. }
  223. }
  224. }
  225. if (!_replacerFunction.IsUndefined())
  226. {
  227. var replacerFunctionCallable = (ICallable) _replacerFunction.AsObject();
  228. value = replacerFunctionCallable.Call(holder, TypeConverter.ToPropertyKey(key), value);
  229. }
  230. if (value.IsObject())
  231. {
  232. value = value switch
  233. {
  234. NumberInstance => TypeConverter.ToNumber(value),
  235. StringInstance => TypeConverter.ToString(value),
  236. BooleanInstance booleanInstance => booleanInstance.BooleanData,
  237. BigIntInstance bigIntInstance => bigIntInstance.BigIntData,
  238. _ => value
  239. };
  240. }
  241. return value;
  242. }
  243. private static bool CanSerializesAsArray(ObjectInstance value)
  244. {
  245. if (value is JsArray)
  246. {
  247. return true;
  248. }
  249. if (value is JsProxy proxyInstance && CanSerializesAsArray(proxyInstance._target))
  250. {
  251. return true;
  252. }
  253. if (value is ObjectWrapper { IsArrayLike: true })
  254. {
  255. return true;
  256. }
  257. return false;
  258. }
  259. /// <summary>
  260. /// https://tc39.es/ecma262/#sec-quotejsonstring
  261. /// </summary>
  262. /// <remarks>
  263. /// MethodImplOptions.AggressiveOptimization = 512 which is only exposed in .NET Core.
  264. /// </remarks>
  265. [MethodImpl(MethodImplOptions.AggressiveInlining | (MethodImplOptions) 512)]
  266. private static unsafe void QuoteJSONString(string value, ref ValueStringBuilder json)
  267. {
  268. if (value.Length == 0)
  269. {
  270. json.Append("\"\"");
  271. return;
  272. }
  273. json.Append('"');
  274. #if NETCOREAPP1_0_OR_GREATER
  275. fixed (char* ptr = value)
  276. {
  277. int remainingLength = value.Length;
  278. int offset = 0;
  279. while (true)
  280. {
  281. int index = System.Text.Encodings.Web.JavaScriptEncoder.Default.FindFirstCharacterToEncode(ptr + offset, remainingLength);
  282. if (index < 0)
  283. {
  284. // append the remaining text which doesn't need any encoding.
  285. json.Append(value.AsSpan(offset));
  286. break;
  287. }
  288. index += offset;
  289. if (index - offset > 0)
  290. {
  291. // append everything which does not need any encoding until the found index.
  292. json.Append(value.AsSpan(offset, index - offset));
  293. }
  294. AppendJsonStringCharacter(value, ref index, ref json);
  295. offset = index + 1;
  296. remainingLength = value.Length - offset;
  297. if (remainingLength == 0)
  298. {
  299. break;
  300. }
  301. }
  302. }
  303. #else
  304. for (var i = 0; i < value.Length; i++)
  305. {
  306. AppendJsonStringCharacter(value, ref i, ref json);
  307. }
  308. #endif
  309. json.Append('"');
  310. }
  311. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  312. private static void AppendJsonStringCharacter(string value, ref int index, ref ValueStringBuilder json)
  313. {
  314. var c = value[index];
  315. switch (c)
  316. {
  317. case '\"':
  318. json.Append("\\\"");
  319. break;
  320. case '\\':
  321. json.Append("\\\\");
  322. break;
  323. case '\b':
  324. json.Append("\\b");
  325. break;
  326. case '\f':
  327. json.Append("\\f");
  328. break;
  329. case '\n':
  330. json.Append("\\n");
  331. break;
  332. case '\r':
  333. json.Append("\\r");
  334. break;
  335. case '\t':
  336. json.Append("\\t");
  337. break;
  338. default:
  339. if (char.IsSurrogatePair(value, index))
  340. {
  341. #if NETCOREAPP1_0_OR_GREATER
  342. json.Append(value.AsSpan(index, 2));
  343. index++;
  344. #else
  345. json.Append(c);
  346. index++;
  347. json.Append(value[index]);
  348. #endif
  349. }
  350. else if (c < 0x20 || char.IsSurrogate(c))
  351. {
  352. json.Append("\\u");
  353. json.Append(((int) c).ToString("x4", CultureInfo.InvariantCulture));
  354. }
  355. else
  356. {
  357. json.Append(c);
  358. }
  359. break;
  360. }
  361. }
  362. /// <summary>
  363. /// https://tc39.es/ecma262/#sec-serializejsonarray
  364. /// </summary>
  365. private void SerializeJSONArray(ObjectInstance value, ref ValueStringBuilder json)
  366. {
  367. var len = TypeConverter.ToUint32(value.Get(CommonProperties.Length));
  368. if (len == 0)
  369. {
  370. json.Append("[]");
  371. return;
  372. }
  373. _stack.Enter(value);
  374. var stepback = _indent;
  375. if (_gap.Length > 0)
  376. {
  377. _indent += _gap;
  378. }
  379. const char separator = ',';
  380. bool hasPrevious = false;
  381. for (int i = 0; i < len; i++)
  382. {
  383. if (hasPrevious)
  384. {
  385. json.Append(separator);
  386. }
  387. else
  388. {
  389. json.Append('[');
  390. }
  391. if (_gap.Length > 0)
  392. {
  393. json.Append('\n');
  394. json.Append(_indent);
  395. }
  396. if (SerializeJSONProperty(i, value, ref json) == SerializeResult.Undefined)
  397. {
  398. json.Append("null");
  399. }
  400. hasPrevious = true;
  401. }
  402. if (!hasPrevious)
  403. {
  404. _stack.Exit();
  405. _indent = stepback;
  406. json.Append("[]");
  407. return;
  408. }
  409. if (_gap.Length > 0)
  410. {
  411. json.Append('\n');
  412. json.Append(stepback);
  413. }
  414. json.Append(']');
  415. _stack.Exit();
  416. _indent = stepback;
  417. }
  418. /// <summary>
  419. /// https://tc39.es/ecma262/#sec-serializejsonobject
  420. /// </summary>
  421. private void SerializeJSONObject(ObjectInstance value, ref ValueStringBuilder json)
  422. {
  423. var enumeration = _propertyList is null
  424. ? PropertyEnumeration.FromObjectInstance(value)
  425. : PropertyEnumeration.FromList(_propertyList);
  426. if (enumeration.IsEmpty)
  427. {
  428. json.Append("{}");
  429. return;
  430. }
  431. _stack.Enter(value);
  432. var stepback = _indent;
  433. if (_gap.Length > 0)
  434. {
  435. _indent += _gap;
  436. }
  437. const char separator = ',';
  438. var hasPrevious = false;
  439. for (var i = 0; i < enumeration.Keys.Count; i++)
  440. {
  441. var p = enumeration.Keys[i];
  442. int position = json.Length;
  443. if (hasPrevious)
  444. {
  445. json.Append(separator);
  446. }
  447. else
  448. {
  449. json.Append('{');
  450. }
  451. if (_gap.Length > 0)
  452. {
  453. json.Append('\n');
  454. json.Append(_indent);
  455. }
  456. QuoteJSONString(p.ToString(), ref json);
  457. json.Append(':');
  458. if (_gap.Length > 0)
  459. {
  460. json.Append(' ');
  461. }
  462. if (SerializeJSONProperty(p, value, ref json) == SerializeResult.Undefined)
  463. {
  464. json.Length = position;
  465. }
  466. else
  467. {
  468. hasPrevious = true;
  469. }
  470. }
  471. if (!hasPrevious)
  472. {
  473. _stack.Exit();
  474. _indent = stepback;
  475. json.Append("{}");
  476. return;
  477. }
  478. if (_gap.Length > 0)
  479. {
  480. json.Append('\n');
  481. json.Append(stepback);
  482. }
  483. json.Append('}');
  484. _stack.Exit();
  485. _indent = stepback;
  486. }
  487. private enum SerializeResult
  488. {
  489. NotUndefined,
  490. Undefined
  491. }
  492. private readonly struct PropertyEnumeration
  493. {
  494. private PropertyEnumeration(List<JsValue> keys, bool isEmpty)
  495. {
  496. Keys = keys;
  497. IsEmpty = isEmpty;
  498. }
  499. public static PropertyEnumeration FromList(List<JsValue> keys)
  500. => new PropertyEnumeration(keys, keys.Count == 0);
  501. public static PropertyEnumeration FromObjectInstance(ObjectInstance instance)
  502. {
  503. var allKeys = instance.GetOwnPropertyKeys(Types.String);
  504. RemoveUnserializableProperties(instance, allKeys);
  505. return new PropertyEnumeration(allKeys, allKeys.Count == 0);
  506. }
  507. private static void RemoveUnserializableProperties(ObjectInstance instance, List<JsValue> keys)
  508. {
  509. for (var i = 0; i < keys.Count; i++)
  510. {
  511. var key = keys[i];
  512. var desc = instance.GetOwnProperty(key);
  513. if (desc == PropertyDescriptor.Undefined || !desc.Enumerable)
  514. {
  515. keys.RemoveAt(i);
  516. i--;
  517. }
  518. }
  519. }
  520. public readonly List<JsValue> Keys;
  521. public readonly bool IsEmpty;
  522. }
  523. }