Ejemplo n.º 1
0
        public Task <JsValue> Json()
        {
            if (TryCheckBodyUsed())
            {
                return(Task.FromResult <JsValue>(m_context.Undefined));
            }

            var bodyBytes = ReadFully(m_httpRequest.Body);
            var headers   = m_httpRequest.GetTypedHeaders();
            var bodyText  = headers.ContentType.Encoding.GetString(bodyBytes);

            var result = m_context.JSON.Parse(m_context.CreateString(bodyText));

            m_bodyUsed = true;

            return(Task.FromResult(result));
        }
Ejemplo n.º 2
0
        public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule)
        {
            var transpiledScript = TypeScriptTranspiler.Default.Transpile(new TranspileOptions()
            {
                Script   = Script,
                FileName = m_filename
            }).GetAwaiter().GetResult();

            return(context.CreateString(transpiledScript.OutputText));
        }
Ejemplo n.º 3
0
            public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule)
            {
                var fnResult = context.CreateFunction(new Func <JsObject, JsValue, JsValue>((thisObj, toReverse) =>
                {
                    if (toReverse == null || String.IsNullOrWhiteSpace(toReverse.ToString()))
                    {
                        return(context.Undefined);
                    }

                    var str       = toReverse.ToString();
                    var charArray = str.ToCharArray();
                    Array.Reverse(charArray);
                    var reversed = new string(charArray);

                    return(context.CreateString(reversed));
                }));

                return(fnResult);
            }
Ejemplo n.º 4
0
 public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule)
 {
     return(context.JSON.Parse(context.CreateString(JsonText)));
 }
Ejemplo n.º 5
0
 public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule)
 {
     return(context.CreateString("The maze isn't meant for you."));
 }
Ejemplo n.º 6
0
 public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule)
 {
     return(context.CreateString("You'll not see me."));
 }
Ejemplo n.º 7
0
 public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule)
 {
     return(context.CreateString(referencingModule.Name));
 }
Ejemplo n.º 8
0
 public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule)
 {
     return(context.CreateString("Hello, World!"));
 }
 public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule)
 {
     return(context.CreateString("Goodnight, moon."));
 }
Ejemplo n.º 10
0
        public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule)
        {
            var toXml = context.CreateFunction(new Func <JsObject, JsValue, JsValue>((thisObj, json) =>
            {
                if (json == context.Undefined || json == context.Null || String.IsNullOrWhiteSpace(json.ToString()))
                {
                    var error = context.CreateError($"A Json string must be specified as the first argument.");
                    context.Engine.JsSetException(error.Handle);
                    return(context.Undefined);
                }

                var jsonData = json.ToString();
                var xmlDoc   = JsonConvert.DeserializeXmlNode(jsonData);
                using (var stringWriter = new StringWriter())
                    using (var xmlTextWriter = XmlWriter.Create(stringWriter))
                    {
                        xmlDoc.WriteTo(xmlTextWriter);
                        xmlTextWriter.Flush();
                        return(context.CreateString(stringWriter.GetStringBuilder().ToString()));
                    }
            }));

            var toJson = context.CreateFunction(new Func <JsObject, JsValue, JsValue, JsValue>((thisObj, xml, options) =>
            {
                if (xml == context.Undefined || xml == context.Null || String.IsNullOrWhiteSpace(xml.ToString()))
                {
                    var error = context.CreateError($"An xml string must be specified as the first argument.");
                    context.Engine.JsSetException(error.Handle);
                    return(context.Undefined);
                }

                var xmlData = xml.ToString();
                var xmlDoc  = new XmlDocument();
                xmlDoc.LoadXml(xmlData);

                bool toObject       = false;
                bool omitRootObject = false;
                Newtonsoft.Json.Formatting formatting = Newtonsoft.Json.Formatting.None;
                if (options is JsObject jsOptions && jsOptions.Type == JsValueType.Object)
                {
                    if (jsOptions.HasProperty("object") && jsOptions["object"].ToBoolean() == true)
                    {
                        toObject = true;
                    }

                    if (jsOptions.HasProperty("omitRootObject") && jsOptions["omitRootObject"].ToBoolean() == true)
                    {
                        omitRootObject = true;
                    }

                    if (jsOptions.HasProperty("formatting") && Enum.TryParse(jsOptions["formatting"].ToString(), out Newtonsoft.Json.Formatting requestedFormatting))
                    {
                        formatting = requestedFormatting;
                    }
                }

                var json = JsonConvert.SerializeXmlNode(xmlDoc, formatting, omitRootObject);

                if (toObject)
                {
                    return(context.JSON.Parse(context.CreateString(json)));
                }
                return(context.CreateString(json));
            }));

            var resultObj = context.CreateObject();

            context.Object.DefineProperty(resultObj, "toXml", new JsPropertyDescriptor
            {
                Enumerable = true,
                Value      = toXml
            });

            context.Object.DefineProperty(resultObj, "toJson", new JsPropertyDescriptor
            {
                Enumerable = true,
                Value      = toJson
            });

            return(resultObj);
        }
Ejemplo n.º 11
0
 public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule)
 {
     return(context.CreateString(m_resourceManager.GetString(ResourceName, ResourceCulture)));
 }