123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- using System.Collections.Generic;
- using System.Linq;
- using Jint.Collections;
- using Jint.Native.BigInt;
- using Jint.Native.Boolean;
- using Jint.Native.Global;
- using Jint.Native.Number;
- using Jint.Native.Object;
- using Jint.Native.String;
- using Jint.Pooling;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Interop;
- namespace Jint.Native.Json
- {
- public class JsonSerializer
- {
- private readonly Engine _engine;
- private ObjectTraverseStack _stack;
- private string _indent, _gap;
- private List<JsValue> _propertyList;
- private JsValue _replacerFunction = Undefined.Instance;
- private static readonly JsString toJsonProperty = new("toJSON");
- public JsonSerializer(Engine engine)
- {
- _engine = engine;
- }
- public JsValue Serialize(JsValue value, JsValue replacer, JsValue space)
- {
- _stack = new ObjectTraverseStack(_engine);
- // for JSON.stringify(), any function passed as the first argument will return undefined
- // if the replacer is not defined. The function is not called either.
- if (value.IsCallable && ReferenceEquals(replacer, Undefined.Instance))
- {
- return Undefined.Instance;
- }
- if (replacer.IsObject())
- {
- if (replacer is ICallable)
- {
- _replacerFunction = replacer;
- }
- else
- {
- var replacerObj = replacer.AsObject();
- _propertyList = new List<JsValue>();
- foreach (var pair in replacerObj.GetOwnProperties())
- {
- var v = replacerObj.UnwrapJsValue(pair.Value);
- string item = null;
- if (v.IsString())
- {
- item = v.ToString();
- }
- else if (v.IsNumber())
- {
- item = TypeConverter.ToString(v);
- }
- else if (v.IsObject())
- {
- var propertyObj = v.AsObject();
- if (propertyObj.Class == ObjectClass.String || propertyObj.Class == ObjectClass.Number)
- {
- item = TypeConverter.ToString(v);
- }
- }
- if (item != null && !_propertyList.Contains(item))
- {
- _propertyList.Add(item);
- }
- }
- }
- }
- if (space.IsObject())
- {
- var spaceObj = space.AsObject();
- if (spaceObj.Class == ObjectClass.Number)
- {
- space = TypeConverter.ToNumber(spaceObj);
- }
- else if (spaceObj.Class == ObjectClass.String)
- {
- space = TypeConverter.ToJsString(spaceObj);
- }
- }
- // defining the gap
- if (space.IsNumber())
- {
- var number = ((JsNumber) space)._value;
- if (number > 0)
- {
- _gap = new string(' ', (int) System.Math.Min(10, number));
- }
- else
- {
- _gap = string.Empty;
- }
- }
- else if (space.IsString())
- {
- var stringSpace = space.ToString();
- _gap = stringSpace.Length <= 10 ? stringSpace : stringSpace.Substring(0, 10);
- }
- else
- {
- _gap = string.Empty;
- }
- var wrapper = _engine.Realm.Intrinsics.Object.Construct(Arguments.Empty);
- wrapper.DefineOwnProperty(JsString.Empty, new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable));
- return SerializeJSONProperty(JsString.Empty, wrapper);
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-serializejsonproperty
- /// </summary>
- private JsValue SerializeJSONProperty(JsValue key, JsValue holder)
- {
- var value = holder.Get(key, holder);
- var isBigInt = value is BigIntInstance || value.IsBigInt();
- if (value.IsObject() || isBigInt)
- {
- var toJson = value.Get(toJsonProperty, value);
- if (toJson.IsUndefined() && isBigInt)
- {
- toJson = _engine.Realm.Intrinsics.BigInt.PrototypeObject.Get(toJsonProperty);
- }
- if (toJson.IsObject())
- {
- if (toJson.AsObject() is ICallable callableToJson)
- {
- value = callableToJson.Call(value, Arguments.From(key));
- }
- }
- }
- if (!_replacerFunction.IsUndefined())
- {
- var replacerFunctionCallable = (ICallable) _replacerFunction.AsObject();
- value = replacerFunctionCallable.Call(holder, Arguments.From(key, value));
- }
- if (value.IsObject())
- {
- var valueObj = value.AsObject();
- switch (valueObj)
- {
- case NumberInstance numberInstance:
- value = numberInstance.NumberData;
- break;
- case StringInstance stringInstance:
- value = stringInstance.StringData;
- break;
- case BooleanInstance booleanInstance:
- value = booleanInstance.BooleanData;
- break;
- case BigIntInstance bigIntInstance:
- value = bigIntInstance.BigIntData;
- break;
- }
- }
- if (ReferenceEquals(value, Null.Instance))
- {
- return JsString.NullString;
- }
- if (value.IsBoolean())
- {
- return ((JsBoolean) value)._value ? JsString.TrueString : JsString.FalseString;
- }
- if (value.IsString())
- {
- return QuoteJSONString(value.ToString());
- }
- if (value.IsNumber())
- {
- var isFinite = GlobalObject.IsFinite(Undefined.Instance, Arguments.From(value));
- if (((JsBoolean) isFinite)._value)
- {
- return TypeConverter.ToJsString(value);
- }
- return JsString.NullString;
- }
- if (value.IsBigInt())
- {
- ExceptionHelper.ThrowTypeError(_engine.Realm, "Do not know how to serialize a BigInt");
- }
- var isCallable = value.IsObject() && value.IsCallable;
- if (value.IsObject() && isCallable == false)
- {
- return SerializesAsArray(value)
- ? SerializeJSONArray(value)
- : SerializeJSONObject(value.AsObject());
- }
- return JsValue.Undefined;
- }
- private static bool SerializesAsArray(JsValue value)
- {
- return value.AsObject().Class == ObjectClass.Array || value is ObjectWrapper { IsArrayLike: true };
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-quotejsonstring
- /// </summary>
- private static string QuoteJSONString(string value)
- {
- using var stringBuilder = StringBuilderPool.Rent();
- var sb = stringBuilder.Builder;
- sb.Append("\"");
- foreach (var c in value)
- {
- switch (c)
- {
- case '\"':
- sb.Append("\\\"");
- break;
- case '\\':
- sb.Append("\\\\");
- break;
- case '\b':
- sb.Append("\\b");
- break;
- case '\f':
- sb.Append("\\f");
- break;
- case '\n':
- sb.Append("\\n");
- break;
- case '\r':
- sb.Append("\\r");
- break;
- case '\t':
- sb.Append("\\t");
- break;
- default:
- if (c < 0x20)
- {
- sb.Append("\\u");
- sb.Append(((int) c).ToString("x4"));
- }
- else
- sb.Append(c);
- break;
- }
- }
- sb.Append("\"");
- return sb.ToString();
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-serializejsonarray
- /// </summary>
- private string SerializeJSONArray(JsValue value)
- {
- _stack.Enter(value);
- var stepback = _indent;
- _indent = _indent + _gap;
- var partial = new List<string>();
- var len = TypeConverter.ToUint32(value.Get(CommonProperties.Length, value));
- for (int i = 0; i < len; i++)
- {
- var strP = SerializeJSONProperty(i, value);
- if (strP.IsUndefined())
- {
- strP = JsString.NullString;
- }
- partial.Add(strP.ToString());
- }
- if (partial.Count == 0)
- {
- _stack.Exit();
- return "[]";
- }
- string final;
- if (_gap == "")
- {
- const string separator = ",";
- var properties = string.Join(separator, partial);
- final = "[" + properties + "]";
- }
- else
- {
- var separator = ",\n" + _indent;
- var properties = string.Join(separator, partial);
- final = "[\n" + _indent + properties + "\n" + stepback + "]";
- }
- _stack.Exit();
- _indent = stepback;
- return final;
- }
- /// <summary>
- /// https://tc39.es/ecma262/#sec-serializejsonobject
- /// </summary>
- private string SerializeJSONObject(ObjectInstance value)
- {
- string final;
- _stack.Enter(value);
- var stepback = _indent;
- _indent += _gap;
- var k = _propertyList ?? value.GetOwnProperties()
- .Where(x => !x.Key.IsSymbol() && x.Value.Enumerable)
- .Select(x => x.Key);
- var partial = new List<string>();
- foreach (var p in k)
- {
- var strP = SerializeJSONProperty(p, value);
- if (!strP.IsUndefined())
- {
- var member = QuoteJSONString(p.ToString()) + ":";
- if (_gap != "")
- {
- member += " ";
- }
- member += strP.AsString(); // TODO:This could be undefined
- partial.Add(member);
- }
- }
- if (partial.Count == 0)
- {
- final = "{}";
- }
- else
- {
- if (_gap == "")
- {
- const string separator = ",";
- var properties = string.Join(separator, partial);
- final = "{" + properties + "}";
- }
- else
- {
- var separator = ",\n" + _indent;
- var properties = string.Join(separator, partial);
- final = "{\n" + _indent + properties + "\n" + stepback + "}";
- }
- }
- _stack.Exit();
- _indent = stepback;
- return final;
- }
- }
- }
|