Skip to content

NullReferenceException running script (x86 Win10, .NET 4.6) #1095

@aikeru

Description

@aikeru

I love ScriptCS! :)

This code, when run within a much larger script, seems to run fine on a Windows 8.1 64-bit system with .NET 4.5.2 and Visual Studio 2015 installed.

It throws a NullReferenceException when run on a Windows 10, 32-bit system with what seems to be .NET 4.6. Visual Studio is not installed.

Please let me know if I can supply any additional details for troubleshooting.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections;

    public class JSONObjectBase
    {
        private System.Collections.Generic.Dictionary<string, JSONObjectBase> _JSONProperties = new System.Collections.Generic.Dictionary<string, JSONObjectBase>();

        public System.Collections.Generic.Dictionary<string, JSONObjectBase> JSONProperties
        {
            get { return _JSONProperties; }
            set { _JSONProperties = value; }
        }

        /// <summary>
        /// Will not be used if JSONProperties.Count > 0
        /// </summary>
        public object Value { get; set; }

        public JSONObjectBase Attr(string name)
        {
            return JSONProperties[name];
        }

        public override string ToString()
        {
            return Render();
        }

        public virtual string RenderJSONValue(object val)
        {
            StringBuilder sb = new StringBuilder();
            if (val == null) { return "null"; }
            if (val.GetType() == typeof(bool))
            {
                if (Convert.ToBoolean(val)) { return "true"; }
                return "false";
            }
            Type[] numberArrays = new Type[] {
                    typeof(int[]), typeof(long[]),typeof(double[]),typeof(float[]),typeof(byte[]),typeof(short[]),typeof(decimal[])
                };
            Type[] numbers = new Type[] {
                   typeof(int), typeof(long), typeof(double), typeof(float), typeof(byte),typeof(short),typeof(decimal)
                };
            if (numberArrays.Contains(val.GetType()))
            {
                sb.Append("[");
                bool firstNum = true;
                foreach (object x in (IEnumerable)val)
                {
                    if (firstNum) { sb.Append(x.ToString()); }
                    if (!firstNum) { sb.Append("," + x.ToString()); }
                    firstNum = false;

                }
                sb.Append("]");
            }
            else if (val.GetType() == typeof(string))
            {
                return "\"" + val.ToString() + "\"";
            }
            else if (val as IEnumerable != null)
            {
                sb.Append("[");
                bool firstArr = true;
                foreach (object x in (IEnumerable)val)
                {
                    if (firstArr) { sb.Append(RenderJSONValue(x)); }
                    if (!firstArr) { sb.Append("," + RenderJSONValue(x)); }
                    firstArr = false;
                }
                sb.Append("]");
            }
            else if (val.GetType().IsSubclassOf(typeof(JSONObjectBase)))
            {
                sb.Append(((JSONObjectBase)val).Render());
            }
            else if (numbers.Contains(val.GetType()))
            {
                sb.Append(val.ToString());
            }
            else
            {
                sb.Append("\"" + val.ToString() + "\"");
            }
            return sb.ToString();
        }

        public virtual string Render()
        {
            StringBuilder sb = new StringBuilder();
            bool firstProp = true;
            if (JSONProperties.Count > 0)
            {
                sb.Append("{");
                foreach (KeyValuePair<string, JSONObjectBase> kvp in JSONProperties)
                {
                    if (firstProp)
                    {
                        sb.Append("\"" + kvp.Key + "\":");
                        firstProp = false;
                    }
                    else
                    {
                        sb.Append(",\"" + kvp.Key + "\":");
                    }
                    sb.Append(kvp.Value.Render());
                }
                sb.Append("}");
            }
            else
            {
                return RenderJSONValue(Value);
            }

            var output = sb.ToString();
            output = output.Replace("\r\n", "\\r\\n")
            .Replace("\r", "\\r")
            .Replace("\n", "\\n");
            //if (sb.ToString().Contains("\r") || sb.ToString().Contains("\n"))
            //{ throw new Exception("JSON cannot contain return characters \\r \\n."); }

            return output;
        }

        public virtual JSONObjectBase MergeObjectProperties<T>(System.Collections.Generic.Dictionary<string, T> jsonProperties) {
            if (jsonProperties == null) { return this; }
            if (jsonProperties.Count < 1) { return this; }
            foreach (KeyValuePair<string, T> kvp in jsonProperties) {
                if (JSONProperties.ContainsKey(kvp.Key.ToLower())) {
                    JSONProperties[kvp.Key.ToLower()] = new JSONObject(jsonProperties[kvp.Key]);
                } else {
                    JSONProperties.Add(kvp.Key.ToLower(), new JSONObject(jsonProperties[kvp.Key]));
                }
            }
            return this;
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions