Exemple #1
0
 private EcmaValue TransformValue(RuntimeObject holder, EcmaPropertyKey property, EcmaValue value)
 {
     if (value.Type == EcmaValueType.Object)
     {
         EcmaValue toJson = value[WellKnownProperty.ToJSON];
         if (toJson.IsCallable)
         {
             value = toJson.Call(value, property.ToValue());
         }
     }
     if (replacer != EcmaValue.Undefined)
     {
         value = replacer.Call(holder, property.ToValue(), value);
     }
     if (value.Type == EcmaValueType.Object)
     {
         EcmaValue primitive = EcmaValueUtility.UnboxPrimitiveObject(value);
         switch (primitive.Type)
         {
         case EcmaValueType.Number:
         case EcmaValueType.String:
         case EcmaValueType.Boolean:
             return(primitive);
         }
     }
     return(value);
 }
Exemple #2
0
        public static EcmaValue Stringify(EcmaValue value, EcmaValue replacer, EcmaValue space)
        {
            string indentString;

            space = EcmaValueUtility.UnboxPrimitiveObject(space);
            if (space.Type == EcmaValueType.Number)
            {
                indentString = new String(' ', Math.Max(0, Math.Min(10, space.ToInt32())));
            }
            else if (space.Type == EcmaValueType.String)
            {
                indentString = space.ToStringOrThrow();
            }
            else
            {
                indentString = String.Empty;
            }
            string result;

            if (replacer.IsCallable)
            {
                result = new EcmaJsonWriter(indentString, replacer).Serialize(value);
            }
            else if (EcmaArray.IsArray(replacer))
            {
                HashSet <string> propertyList = new HashSet <string>();
                for (long i = 0, length = replacer[WellKnownProperty.Length].ToLength(); i < length; i++)
                {
                    EcmaValue item = EcmaValueUtility.UnboxPrimitiveObject(replacer[i]);
                    if (item.Type == EcmaValueType.String || item.Type == EcmaValueType.Number)
                    {
                        propertyList.Add(item.ToStringOrThrow());
                    }
                }
                result = new EcmaJsonWriter(indentString, propertyList).Serialize(value);
            }
            else
            {
                result = new EcmaJsonWriter(indentString).Serialize(value);
            }
            return(result ?? EcmaValue.Undefined);
        }