Beispiel #1
0
 /// <summary>
 /// Create encoder
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="context"></param>
 /// <param name="encoding"></param>
 /// <param name="formatting"></param>
 public JsonEncoderEx(Stream stream,
                      ServiceMessageContext context         = null, JsonEncoding encoding = JsonEncoding.Object,
                      Newtonsoft.Json.Formatting formatting = Newtonsoft.Json.Formatting.None) :
     this(new StreamWriter(stream, new UTF8Encoding(false)),
          context, encoding, formatting)
 {
 }
Beispiel #2
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiRegistrar.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            //GlobalConfiguration.Configuration.MessageHandlers.Add(new eXtensibleMessageHandler() { CreateClaimsPrincipal = () => { return new eXtensibleClaimsPrincipal<WebApiCaller>(new ClaimsIdentity()); } });
            var handler = new eXtensibleMessageHandler();

            GlobalConfiguration.Configuration.MessageHandlers.Add(new eXtensibleMessageHandler());
            GlobalConfiguration.Configuration.MessageHandlers.Add(new BlackRocketMessageHandler());

            var formatters   = GlobalConfiguration.Configuration.Formatters;
            var xmlFormatter = formatters.XmlFormatter;

            formatters.Remove(xmlFormatter);
            var jsonFormatter = formatters.JsonFormatter;

            Newtonsoft.Json.Formatting formatting = Formatting.None;
            formatting = Formatting.Indented;

            var settings = new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting           = formatting,
                ContractResolver     = new CamelCasePropertyNamesContractResolver(),
                DateTimeZoneHandling = DateTimeZoneHandling.Utc,
                NullValueHandling    = NullValueHandling.Ignore
            };

            jsonFormatter.SerializerSettings = settings;
        }
Beispiel #3
0
        public static ActionResult ToMvcJson(this object obj, Newtonsoft.Json.Formatting formatting, JsonSerializerSettings settings)
        {
            ContentResult con = new ContentResult();

            con.Content = JsonConvert.SerializeObject(obj, formatting, settings);
            return(con);
        }
Beispiel #4
0
        public static int Run(RewriteOptions rewriteOptions)
        {
            try
            {
                rewriteOptions = ValidateOptions(rewriteOptions);
                string fileName = GetOutputFileName(rewriteOptions);

                Newtonsoft.Json.Formatting formatting = rewriteOptions.PrettyPrint
                    ? Newtonsoft.Json.Formatting.Indented
                    : Newtonsoft.Json.Formatting.None;

                JsonSerializerSettings settings = new JsonSerializerSettings()
                {
                    ContractResolver = SarifContractResolver.Instance,
                    Formatting       = Newtonsoft.Json.Formatting.Indented
                };

                string   sarifText = File.ReadAllText(rewriteOptions.InputFilePath);
                SarifLog actualLog = JsonConvert.DeserializeObject <SarifLog>(sarifText, settings);

                LoggingOptions loggingOptions = rewriteOptions.ConvertToLoggingOptions();

                SarifLog reformattedLog = new ReformattingVisitor(loggingOptions).VisitSarifLog(actualLog);

                File.WriteAllText(fileName, JsonConvert.SerializeObject(reformattedLog, settings));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return(1);
            }

            return(0);
        }
Beispiel #5
0
 /// <summary>
 /// 序列化物件
 /// </summary>
 /// <param name="obj">傳入的物件</param>
 /// <param name="formatting">格式化</param>
 /// <returns></returns>
 public static string ToJsonString(object obj, Newtonsoft.Json.Formatting formatting = Newtonsoft.Json.Formatting.None, NullValueHandling nullValueHandling = NullValueHandling.Ignore)
 {
     return(JsonConvert.SerializeObject(obj, formatting, new JsonSerializerSettings()
     {
         NullValueHandling = nullValueHandling
     }));
 }
Beispiel #6
0
        public static string XmlToJson
        (
            string xml
            , Newtonsoft.Json.Formatting formatting = Newtonsoft.Json.Formatting.Indented
            , bool keyQuoteName = false
        )
        {
            XNode xElement;

            xElement = XElement.Parse(xml).Elements().First();
            string json = string.Empty;

            using (var stringWriter = new StringWriter())
            {
                using (var jsonTextWriter = new JsonTextWriter(stringWriter))
                {
                    jsonTextWriter.Formatting = formatting;
                    jsonTextWriter.QuoteName  = keyQuoteName;
                    JsonSerializer jsonSerializer = new JsonSerializer();
                    jsonSerializer.Serialize(jsonTextWriter, xElement);
                    json = stringWriter.ToString();
                }
            }
            return(json);
        }
Beispiel #7
0
 public static string JsonSerialize(this object o, Formatting formatting = Formatting.None)
 {
     return(JsonConvert.SerializeObject(o, formatting, new JsonSerializerSettings
     {
         NullValueHandling = NullValueHandling.Ignore
     }));
 }
Beispiel #8
0
 /// <summary>
 /// 将 <see cref="List{T}"/> 对象序列化成 Json 字符串
 /// </summary>
 /// <typeparam name="T">要序列化的集合的类型</typeparam>
 /// <param name="list">要序列化的 <see cref="List{T}"/></param>
 /// <param name="formatting">序列化的格式</param>
 /// <param name="settings">序列化的设置</param>
 /// <returns>如果 dataTable 参数为 null,则返回 <see cref="string.Empty"/>;否则返回序列化后的 json 字符串。</returns>
 public string SerializeListToJson <T>(List <T> list, Newtonsoft.Json.Formatting formatting, JsonSerializerSettings settings)
 {
     if (list == null)
     {
         return(string.Empty);
     }
     return(JsonConvert.SerializeObject(list, formatting, settings));
 }
Beispiel #9
0
 /// <summary>
 /// 将 <see cref="List{T}"/> 对象序列化成 Json 字符串
 /// </summary>
 /// <typeparam name="T">要序列化的集合的类型</typeparam>
 /// <param name="list">要序列化的 <see cref="List{T}"/></param>
 /// <param name="formatting">序列化的格式</param>
 /// <param name="converters">序列化时使用的转换器的集合</param>
 /// <returns>如果 dataTable 参数为 null,则返回 <see cref="string.Empty"/>;否则返回序列化后的 json 字符串。</returns>
 public string SerializeListToJson <T>(List <T> list, Newtonsoft.Json.Formatting formatting, params JsonConverter[] converters)
 {
     if (list == null)
     {
         return(string.Empty);
     }
     return(JsonConvert.SerializeObject(list, formatting, converters));
 }
Beispiel #10
0
 public static string Serialize(object obj, Formatting formatting, List <JsonConverter> converters, bool quoteName, IContractResolver resolver)
 {
     return(JsonConvert.SerializeObject(obj, formatting, new JsonSerializerSettings {
         ContractResolver = resolver,
         Converters = converters,
         ReferenceLoopHandling = ReferenceLoopHandling.Ignore
     }));
 }
Beispiel #11
0
 /// <summary>
 /// 将 <see cref="DataSet"/> 对象序列化成 Json 字符串
 /// </summary>
 /// <param name="dataSet">要序列化的 <see cref="DataSet"/></param>
 /// <param name="formatting">序列化的格式</param>
 /// <param name="converters">序列化时使用的转换器的集合</param>
 /// <returns>如果 dataSet 参数为 null,则返回 <see cref="string.Empty"/>;否则返回序列化后的 json 字符串。</returns>
 public string SerializeDataSetToJson(DataSet dataSet, Newtonsoft.Json.Formatting formatting, params JsonConverter[] converters)
 {
     if (dataSet == null)
     {
         return(string.Empty);
     }
     return(JsonConvert.SerializeObject(dataSet, formatting, converters));
 }
Beispiel #12
0
 /// <summary>
 /// 将 <see cref="DataSet"/> 对象序列化成 Json 字符串
 /// </summary>
 /// <param name="dataSet">要序列化的 <see cref="DataSet"/></param>
 /// <param name="formatting">序列化的格式</param>
 /// <param name="settings">序列化的设置</param>
 /// <returns>如果 dataSet 参数为 null,则返回 <see cref="string.Empty"/>;否则返回序列化后的 json 字符串。</returns>
 public string SerializeDataSetToJson(DataSet dataSet, Newtonsoft.Json.Formatting formatting, JsonSerializerSettings settings)
 {
     if (dataSet == null)
     {
         return(string.Empty);
     }
     return(JsonConvert.SerializeObject(dataSet, formatting, settings));
 }
Beispiel #13
0
 /// <summary>
 /// 序列化
 /// </summary>
 /// <param name="data">要序列化的数据</param>
 /// <returns>序列化后的JsonString字符串</returns>
 public static string Serialize(object data, Newtonsoft.Json.Formatting formatting = Formatting.None)
 {
     if (data == null || string.IsNullOrEmpty(data.ToString()))
     {
         return("{}");
     }
     return(JsonConvert.SerializeObject(data, formatting, timeFormat));
 }
Beispiel #14
0
 /// <summary>
 /// 指定格式序列化JSON
 /// </summary>
 /// <param name="o"></param>
 /// <param name="f"></param>
 /// <returns></returns>
 public static string SerializeObject(this object o, Newtonsoft.Json.Formatting f, IsoDateTimeConverter timeFormat)
 {
     if (o == null)
     {
         return(string.Empty);
     }
     return(Newtonsoft.Json.JsonConvert.SerializeObject(o, f, timeFormat));
 }
Beispiel #15
0
 /// <summary>
 /// 指定格式序列化JSON
 /// </summary>
 /// <param name="o"></param>
 /// <param name="f"></param>
 /// <returns></returns>
 public static string SerializeObject(this object o, Newtonsoft.Json.Formatting f)
 {
     if (o == null)
     {
         return(string.Empty);
     }
     return(Newtonsoft.Json.JsonConvert.SerializeObject(o, f));
 }
Beispiel #16
0
 /// <summary>
 /// 将实体序列化成 Json 字符串
 /// </summary>
 /// <typeparam name="TEntity">要序列化的实体的类型</typeparam>
 /// <param name="entity">要序列化的实体</param>
 /// <param name="formatting">序列化的格式</param>
 /// <returns>如果 entity 参数为 null,则返回 <see cref="string.Empty"/>;否则返回序列化后的 json 字符串。</returns>
 public string SerializeEntityToJson <TEntity>(TEntity entity, Newtonsoft.Json.Formatting formatting) where TEntity : class
 {
     if (entity == null)
     {
         return(string.Empty);
     }
     return(JsonConvert.SerializeObject(entity, formatting));
 }
Beispiel #17
0
        /// <summary>
        /// 将 <see cref="XNode"/> 对象序列化成 Json 字符串
        /// </summary>
        /// <param name="node">要序列化的 <see cref="XNode"/> </param>
        /// <param name="formatting">序列化的格式</param>
        /// <param name="omitRootObject">是否省略根对象的写入</param>
        /// <returns>如果 node 参数为 null,则返回 <see cref="string.Empty"/>;否则返回序列化后的 json 字符串。</returns>
        public string SerializeXmlToJson(XNode node, Newtonsoft.Json.Formatting formatting, bool omitRootObject)
        {
            if (node == null)
            {
                return(string.Empty);
            }

            return(JsonConvert.SerializeXNode(node, formatting, omitRootObject));
        }
Beispiel #18
0
        /// <summary>
        /// 将 <see cref="XDocument"/> 对象序列化成 Json 字符串
        /// </summary>
        /// <param name="xDocument">要序列化的 <see cref="XDocument"/> </param>
        /// <param name="formatting">序列化的格式</param>
        /// <returns>如果 xDocument 参数为 null,则返回 <see cref="string.Empty"/>;否则返回序列化后的 json 字符串。</returns>
        public string SerializeXmlToJson(XDocument xDocument, Newtonsoft.Json.Formatting formatting)
        {
            if (xDocument == null)
            {
                return(string.Empty);
            }

            return(JsonConvert.SerializeXNode(xDocument, formatting));
        }
Beispiel #19
0
        /// <summary>
        /// 将 <see cref="XmlNode"/> 对象序列化成 Json 字符串
        /// </summary>
        /// <param name="node">要序列化的 XML 文档中的单个节点</param>
        /// <param name="formatting">序列化的格式</param>
        /// <returns>如果 node 参数为 null,则返回 <see cref="string.Empty"/>;否则返回序列化后的 json 字符串。</returns>
        public string SerializeXmlToJson(XmlNode node, Newtonsoft.Json.Formatting formatting)
        {
            if (node == null)
            {
                return(string.Empty);
            }

            return(JsonConvert.SerializeXmlNode(node, formatting));
        }
Beispiel #20
0
        /// <summary>
        /// 将 <see cref="DataTable"/> 对象序列化成 Json 数组字符串
        /// </summary>
        /// <param name="dataTable">要序列化的 <see cref="DataTable"/></param>
        /// <param name="formatting">序列化的格式</param>
        /// <param name="converters">序列化时使用的转换器的集合</param>
        /// <returns>如果 dataTable 参数为 null,则返回 <see cref="string.Empty"/>;否则返回序列化后的 json 字符串。</returns>
        public string SerializeDataTableToJsonArray(DataTable dataTable, Newtonsoft.Json.Formatting formatting, params JsonConverter[] converters)
        {
            if (dataTable == null || dataTable.Rows.Count <= 0)
            {
                return(string.Empty);
            }

            return(JsonConvert.SerializeObject(dataTable, formatting, converters));
        }
Beispiel #21
0
        public static string SerializeToJson <T>(this T obj, Newtonsoft.Json.Formatting formatting = Formatting.None)
        {
            if (obj == null)
            {
                return(String.Empty);
            }

            return(JsonConvert.SerializeObject(obj, formatting, DefaultJsonSerializerSettings));
        }
Beispiel #22
0
 public string ToJson(Newtonsoft.Json.Formatting formatting = Formatting.None)
 {
     return(Newtonsoft.Json.JsonConvert.SerializeObject(new
     {
         this.AverageSentenceLength,
         this.Nodes,
         this.StartingWords
     }, formatting));
 }
        public static string JsonSerialize(object value, Newtonsoft.Json.Formatting formatting)
        {
            JsonSerializerSettings jsetting = new JsonSerializerSettings();

            jsetting.DefaultValueHandling = DefaultValueHandling.Ignore;
            jsetting.NullValueHandling    = NullValueHandling.Ignore;

            return(JsonConvert.SerializeObject(value, formatting, jsetting));
        }
Beispiel #24
0
 /// <summary>
 /// Create encoder
 /// </summary>
 /// <param name="writer"></param>
 /// <param name="context"></param>
 /// <param name="encoding"></param>
 /// <param name="formatting"></param>
 public JsonEncoderEx(TextWriter writer,
                      ServiceMessageContext context         = null, JsonEncoding encoding = JsonEncoding.Object,
                      Newtonsoft.Json.Formatting formatting = Newtonsoft.Json.Formatting.None) :
     this(new JsonTextWriter(writer) {
     AutoCompleteOnClose = true,
     DateFormatHandling = DateFormatHandling.IsoDateFormat,
     FloatFormatHandling = FloatFormatHandling.String,
     Formatting = formatting
 }, context, encoding)
 {
 }
Beispiel #25
0
 /// <summary>
 /// Serialize a generic object to a JSON string
 /// </summary>
 /// <param name="value">Any DotNet object</param>
 /// <param name="jsonFormat"></param>
 /// <returns></returns>
 public static string ToStringJson(this object value, Newtonsoft.Json.Formatting jsonFormat = Newtonsoft.Json.Formatting.None)
 {
     try
     {
         return(JsonConvert.SerializeObject(value, jsonFormat));
     }
     catch (Exception e)
     {
         log.Error(e.Message, e);
         throw;
     }
 }
        public void SaveToJson(string filePath, bool prettyPrint = true)
        {
            Newtonsoft.Json.Formatting formatting = prettyPrint
                ? Newtonsoft.Json.Formatting.Indented
                : Newtonsoft.Json.Formatting.None;

            var settings = new JsonSerializerSettings
            {
                Formatting = formatting
            };

            File.WriteAllText(filePath, JsonConvert.SerializeObject(this, settings));
        }
Beispiel #27
0
        public string ToJson(object value, Newtonsoft.Json.Formatting formating = Newtonsoft.Json.Formatting.None)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            var settings = new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            };

            return(JsonConvert.SerializeObject(value, formating, settings));
        }
Beispiel #28
0
 public JsonEndpoint(ImportEngine engine, XmlNode node)
     : base(engine, node)
 {
     FileName = engine.Xml.CombinePath(node.ReadStr("@file"));
     //pwOutputProvider = OutputStreamProviderBase.Create(null, node);
     LineSeparator = node.ReadStrRaw("@linesep", _XmlRawMode.Trim | _XmlRawMode.DefaultOnNull, "\r\n");
     Formatting    = node.ReadBool("@formatted", false) ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None;
     Root          = node.ReadStr("@root", null);
     Array         = node.ReadBool("@array", Root != null);
     if (Root != null && !Array)
     {
         throw new BMNodeException(node, "Cannot have array=[false] when root=[{0}].", Root);
     }
 }
        protected string JsonSerialize(object objectToSerialize, bool prettyPrint = false, JsonSerializerSettings settings = null)
        {
            if (settings == null)
            {
                settings = new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                };
            }

            Newtonsoft.Json.Formatting jsonFormatting = prettyPrint ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None;

            return(JsonConvert.SerializeObject(objectToSerialize, jsonFormatting, settings));
        }
Beispiel #30
0
        public static string SerializeNewtonJson <T>(T value,
                                                     Formatting formating = Formatting.Indented,
                                                     JsonSerializerSettings jsonSerializerSettings = null)
            where T : class, new()
        {
            var output = JsonConvert.SerializeObject(value
                                                     , formating
                                                     , jsonSerializerSettings
                                                     ?? new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            return(output);
        }/*End of SerializeNewtonJson method*/
        /// <summary>
        /// This method directly serialize XML into JSON. Faster for use; however, less reliable than GetJsonFromRequest
        /// due to not entity structure verified.
        /// This is not to be used currently, for internal object names (like "BaseAddress" and "Line" are not removed)
        /// </summary>
        /// <returns>JSON string parsed directly from XML</returns>
        public string GetJsonFromXml()
        {
            if (this.jsonFromXml != null)
                return this.jsonFromXml;

            if (this.xmlDocument == null)
                return null;

            var xElement = XElement.Parse(this.xmlDocument.OuterXml);
            xElement.Descendants().Where(e => string.IsNullOrEmpty(e.Value)).Remove();
            var cleanedXmlDocument = new XmlDocument();
            cleanedXmlDocument.LoadXml(xElement.ToString());
            var formatting = new Newtonsoft.Json.Formatting();

            // TODO
            // Need to remove internal object names, like BaseAddress, Line
            this.jsonFromXml = JsonConvert.SerializeXmlNode(cleanedXmlDocument, formatting, true);
            return this.jsonFromXml;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonEventTextFormatter" /> class.
 /// </summary>
 /// <param name="formatting">The <see cref="EventTextFormatting" /> formatting.</param>
 /// <param name="dateTimeFormat">The date time format used for timestamp value.</param>
 public JsonEventTextFormatter(EventTextFormatting formatting = DefaultEventTextFormatting, string dateTimeFormat = null)
 {
     this.formatting = (Newtonsoft.Json.Formatting)formatting;
     this.DateTimeFormat = dateTimeFormat;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonEventTextFormatter" /> class.
 /// </summary>
 /// <param name="formatting">The <see cref="EventTextFormatting" /> formatting.</param>
 /// <param name="dateTimeFormat">The date time format used for timestamp value.</param>
 public JsonEventTextFormatter(EventTextFormatting formatting = DefaultEventTextFormatting, string dateTimeFormat = null)
 {
     this.formatting = (Newtonsoft.Json.Formatting)formatting;
     this.DateTimeFormat = dateTimeFormat;
     this.IncludeEntrySeparator = true;
 }