Example #1
0
        /// <summary>
        /// Makes a <see cref="JsonBuilder"/> for the given object
        /// </summary>
        /// <param name="instance">The instance to build the <see cref="JsonBuilder"/> from</param>
        /// <param name="settings">The builder settings to use while constructing the <see cref="Json"/> (optional, null will give you the default settings)</param>
        /// <returns>A <see cref="JsonBuilder"/> for you to chain construction commands to in order to form <see cref="Json"/>.</returns>
        public static Json BuildJsonForObject(object instance, JsonBuilder.Settings settings = null)
        {
            JsonBuilder output = MakeRootBuilder(settings);

            if (instance != null)
            {
                FillOutJsonBuilderForObject(instance, output);
            }

            return(output.Finalize());
        }
Example #2
0
        internal static int EvaluteBegginningTabOffset(JsonValue source, JsonBuilder.Settings settings)
        {
            var capture = RegexHelper.Match(source.StringValue, (source.IsDocument ? "{" : @"\[") + @" *(\s*)").GetMostSpecificCapture();

            if (capture == null)
            {
                Logger.LogInfo("Unable to determine current tabbing due to weirdly formatted start. If the document is formatted unexpectedly, that may lead to serious failures. Also, as a result of this error, tabbing may look funny.");
            }
            else
            {
                // Figure out the current tabbing
                string tabText = capture.Value;

                if (tabText == String.Empty)
                {
                    return(0);
                }
                // If the document seems to use the same tabbing as the settings asked for, then use that
                else if (tabText.Contains(settings.Tabbing))
                {
                    return(tabText.NumberOfTimesContained(settings.Tabbing));
                }
                // Otherwise we'll figure out a best guess for tabbing
                else
                {
                    // If the tabbing seems to use tabs, then number of tabs will do
                    if (tabText[0] == '\t')
                    {
                        return(tabText.Length);
                    }
                    // If tabbing seems to use some othe sequence, then we'll say every 4
                    //  count as one tab.
                    else
                    {
                        return(tabText.Length / 4);
                    }
                }
            }

            // If we couldn't figure anything out anywhere, default to 1
            return(1);
        }
Example #3
0
 public JsonValue AppendNew(object value, JsonBuilder.Settings settings = null)
 {
     return(AppendNew(JsonHelper.MakeValueBuilder(value, settings)));
 }
Example #4
0
 static JsonHelper()
 {
     DefaultSettings = new JsonBuilder.Settings();
 }
Example #5
0
 internal static JsonBuilder MakeValueBuilder(object value, JsonBuilder.Settings settings = null)
 {
     return(new JsonBuilder(settings, value));
 }
Example #6
0
 /// <summary>
 /// Starting point if you are building AJson via a builder.
 /// </summary>
 /// <example>
 /// <see cref="JsonHelper"/>.MakeRootBuilder().StartDocument().AddProperty("test", 2).Finalize();
 /// would give you an Json object with a JsonDocument structured like this: { "test": 2 }
 /// </example>
 /// <param name="settings">The builder settings to use while constructing the <see cref="Json"/> (optional, null will give you the default settings)</param>
 /// <returns>A <see cref="JsonBuilder"/> for you to chain construction commands to in order to form <see cref="Json"/>.</returns>
 public static JsonBuilder MakeRootBuilder(JsonBuilder.Settings settings = null)
 {
     return(new JsonBuilder(settings ?? DefaultSettings));
 }