public JsValue ExportDefault(BaristaContext context, BaristaModuleRecord referencingModule)
        {
            var brewRequest = new BrewRequest(context, m_request);

            context.Converter.TryFromObject(context, brewRequest, out JsValue requestObj);
            context.Converter.TryFromObject(context, typeof(BrewResponse), out JsValue responseObj);
            //context.Converter.TryFromObject(context, m_log, out JsValue logObj);

            var contextObj = context.CreateObject();

            context.Object.DefineProperty(contextObj, "request", new JsPropertyDescriptor()
            {
                Configurable = false,
                Writable     = false,
                Value        = requestObj
            });

            context.Object.DefineProperty(contextObj, "response", new JsPropertyDescriptor()
            {
                Configurable = false,
                Writable     = false,
                Value        = responseObj
            });

            return(contextObj);
        }
        public JsObject GetDescriptorObject(BaristaContext context)
        {
            var descriptor = context.CreateObject();

            if (Configurable)
            {
                descriptor.SetProperty("configurable", context.True);
            }
            if (Enumerable)
            {
                descriptor.SetProperty("enumerable", context.True);
            }
            if (Writable)
            {
                descriptor.SetProperty("enumerable", context.True);
            }

            if (Get != null)
            {
                descriptor.SetProperty("get", Get);
            }

            if (Set != null)
            {
                descriptor.SetProperty("set", Set);
            }

            if (Value != null)
            {
                descriptor.SetProperty("value", Value);
            }

            return(descriptor);
        }
Beispiel #3
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);
        }