Пример #1
0
 /** オブジェクト => JsonItem。
  */
 public static JsonItem ObjectToJsonItem <Type>(Type a_instance, ConvertToJsonItemOption a_option)
 {
     if (a_instance != null)
     {
         return(ConvertObjectToJsonItem.ConvertObjectToJsonItem.Convert(a_instance, a_instance.GetType(), a_option, null, 0));
     }
     else
     {
         return(null);
     }
 }
Пример #2
0
 /** オブジェクト => Json文字列。
  */
 public static void ObjectToJsonString <Type>(Type a_instance, System.Text.StringBuilder a_stringbuilder, ConvertToJsonItemOption a_option_tojsonitem, ConvertToJsonStringOption a_option_tojsonstring)
 {
     ObjectToJsonString_Fee(a_instance, a_stringbuilder, a_option_tojsonitem, a_option_tojsonstring);
 }
Пример #3
0
 /** Fee。オブジェクト => Json文字列。
  */
 public static void ObjectToJsonString_Fee <Type>(Type a_instance, System.Text.StringBuilder a_stringbuilder, ConvertToJsonItemOption a_option_tojsonitem, ConvertToJsonStringOption a_option_tojsonstring)
 {
     if (a_instance != null)
     {
         JsonItem t_jsonitem = ConvertObjectToJsonItem.ConvertObjectToJsonItem.Convert(a_instance, a_instance.GetType(), a_option_tojsonitem, null, 0);
         t_jsonitem.ConvertToJsonString(a_stringbuilder, a_option_tojsonstring);
     }
 }
Пример #4
0
        /** Convert
         */
        public static JsonItem Convert(System.Object a_from_object, ConvertToJsonItemOption a_from_option)
        {
            if ((a_from_option & ConvertToJsonItemOption.EnumString) > 0)
            {
                //enumの文字列化。

                string t_value = a_from_object.ToString();

                if (t_value != null)
                {
                    return(new JsonItem(new Value_StringData(t_value)));
                }
                else
                {
                    //NULL処理。
                    return(null);
                }
            }
            else
            {
                //enumの数値化。

                System.Enum t_enum = (System.Enum)a_from_object;
                if (t_enum != null)
                {
                                        #pragma warning disable 0162
                    switch (t_enum.GetTypeCode())
                    {
                    case System.TypeCode.Byte:
                    {
                        return(new JsonItem(new Value_Number <System.Byte>((System.Byte)a_from_object)));
                    } break;

                    case System.TypeCode.SByte:
                    {
                        return(new JsonItem(new Value_Number <System.SByte>((System.SByte)a_from_object)));
                    } break;

                    case System.TypeCode.Int16:
                    {
                        return(new JsonItem(new Value_Number <System.Int16>((System.Int16)a_from_object)));
                    } break;

                    case System.TypeCode.UInt16:
                    {
                        return(new JsonItem(new Value_Number <System.UInt16>((System.UInt16)a_from_object)));
                    } break;

                    case System.TypeCode.Int32:
                    {
                        return(new JsonItem(new Value_Number <System.Int32>((System.Int32)a_from_object)));
                    } break;

                    case System.TypeCode.UInt32:
                    {
                        return(new JsonItem(new Value_Number <System.UInt32>((System.UInt32)a_from_object)));
                    } break;

                    case System.TypeCode.Int64:
                    {
                        return(new JsonItem(new Value_Number <System.Int64>((System.Int64)a_from_object)));
                    } break;

                    case System.TypeCode.UInt64:
                    {
                        return(new JsonItem(new Value_Number <System.UInt64>((System.UInt64)a_from_object)));
                    } break;

                    default:
                    {
                                                        #if (DEF_BLUEBACK_JSONITEM_ASSERT)
                        DebugTool.Assert(false);
                                                        #endif

                        return(null);
                    } break;
                    }
                                        #pragma warning restore
                }
                else
                {
                                        #if (DEF_BLUEBACK_JSONITEM_ASSERT)
                    DebugTool.Assert(false);
                                        #endif

                    return(null);
                }
            }
        }
Пример #5
0
        /** Add
         *
         *      IndexArray。追加。
         *
         */
        public void Add(ModeAddIndexArray a_mode, JsonItem a_to_jsonitem, System.Object a_from_listitem_object, System.Type a_from_listitem_type, ConvertToJsonItemOption a_from_option, int a_nest)
        {
            WorkPool_Item t_item = new WorkPool_Item();

            {
                //mode
                t_item.mode = (int)a_mode;

                //nest
                t_item.nest = a_nest;

                //コンバート元、インスタンス。
                t_item.from_fieldinfo     = null;
                t_item.from_parent_object = null;
                t_item.from_object        = a_from_listitem_object;
                t_item.from_type          = a_from_listitem_type;
                t_item.from_option        = a_from_option;

                //コンバート先。JSON。
                t_item.to_jsonitem   = a_to_jsonitem;
                t_item.to_index      = 0;
                t_item.to_key_string = null;
            }
            this.list.Add(t_item);
        }
Пример #6
0
        /** Convert
         */
        public static JsonItem Convert(System.Object a_from_object, System.Type a_from_type, ConvertToJsonItemOption a_from_option, ObjectToJsonItem_WorkPool a_workpool, int a_nest)
        {
            ObjectToJsonItem_WorkPool t_workpool = a_workpool;

            if (t_workpool == null)
            {
                t_workpool = new ObjectToJsonItem_WorkPool();
            }

            JsonItem t_to_jsonitem = null;

            try{
                if (a_from_object != null)
                {
                    switch (a_from_type.FullName)
                    {
                    case "System." + nameof(System.String):
                    {
                        System.String t_value = a_from_object as System.String;

                        if (t_value != null)
                        {
                            t_to_jsonitem = new JsonItem(new Value_StringData(t_value));
                        }
                        else
                        {
                            //NULL処理。
                            t_to_jsonitem = null;
                        }
                    } break;

                    case "System." + nameof(System.Char):
                    {
                        t_to_jsonitem = new JsonItem(new Value_Number <System.Char>((System.Char)a_from_object));
                    } break;

                    case "System." + nameof(System.SByte):
                    {
                        t_to_jsonitem = new JsonItem(new Value_Number <System.SByte>((System.SByte)a_from_object));
                    } break;

                    case "System." + nameof(System.Byte):
                    {
                        t_to_jsonitem = new JsonItem(new Value_Number <System.Byte>((System.Byte)a_from_object));
                    } break;

                    case "System." + nameof(System.Int16):
                    {
                        t_to_jsonitem = new JsonItem(new Value_Number <System.Int16>((System.Int16)a_from_object));
                    } break;

                    case "System." + nameof(System.UInt16):
                    {
                        t_to_jsonitem = new JsonItem(new Value_Number <System.UInt16>((System.UInt16)a_from_object));
                    } break;

                    case "System." + nameof(System.Int32):
                    {
                        t_to_jsonitem = new JsonItem(new Value_Number <System.Int32>((System.Int32)a_from_object));
                    } break;

                    case "System." + nameof(System.UInt32):
                    {
                        t_to_jsonitem = new JsonItem(new Value_Number <System.UInt32>((System.UInt32)a_from_object));
                    } break;

                    case "System." + nameof(System.Int64):
                    {
                        t_to_jsonitem = new JsonItem(new Value_Number <System.Int64>((System.Int64)a_from_object));
                    } break;

                    case "System." + nameof(System.UInt64):
                    {
                        t_to_jsonitem = new JsonItem(new Value_Number <System.UInt64>((System.UInt64)a_from_object));
                    } break;

                    case "System." + nameof(System.Single):
                    {
                        t_to_jsonitem = new JsonItem(new Value_Number <System.Single>((System.Single)a_from_object));
                    } break;

                    case "System." + nameof(System.Double):
                    {
                        t_to_jsonitem = new JsonItem(new Value_Number <System.Double>((System.Double)a_from_object));
                    } break;

                    case "System." + nameof(System.Boolean):
                    {
                        t_to_jsonitem = new JsonItem(new Value_Number <System.Boolean>((System.Boolean)a_from_object));
                    } break;

                    case "System." + nameof(System.Decimal):
                    {
                        t_to_jsonitem = new JsonItem(new Value_Number <System.Decimal>((System.Decimal)a_from_object));
                    } break;

                    default:
                    {
                        if (a_from_type.IsArray == true)
                        {
                            //[]
                            t_to_jsonitem = ObjectToJsonItem_FromArray.Convert(a_from_object, a_from_type, a_from_option, t_workpool, a_nest);
                        }
                        else if (a_from_type.IsEnum == true)
                        {
                            //Enum
                            t_to_jsonitem = ObjectToJsonItem_FromEnum.Convert(a_from_object, a_from_option);
                        }
                        else
                        {
                            //class struct generic
                            t_to_jsonitem = ObjectToJsonItem_FromClass.Convert(a_from_object, a_from_type, a_from_option, t_workpool, a_nest);
                        }
                    } break;
                    }
                }
                else
                {
                    //NULL処理。
                }
            }catch (System.Exception t_exception) {
                Tool.DebugReThrow(t_exception);
            }

            //再起呼び出し。
            if (a_workpool == null)
            {
                t_workpool.Main();
            }

            return(t_to_jsonitem);
        }
Пример #7
0
        /** Convert
         */
        public static JsonItem Convert(System.Object a_from_object, System.Type a_from_type, ConvertToJsonItemOption a_from_option, WorkPool a_workpool, int a_nest)
        {
            {
                //IDictionary
                {
                    System.Collections.IDictionary t_from_dictionary = a_from_object as System.Collections.IDictionary;
                    if (t_from_dictionary != null)
                    {
                        System.Type t_list_key_type = ReflectionTool.ReflectionTool.GetDictionaryKeyType(a_from_type);
                        if (t_list_key_type == typeof(string))
                        {
                            //Generic.Dictionary<string.*>
                            //Generic.SortedDictionary<string,*>
                            //Generic.SortedList<string,*>

                            JsonItem t_to_jsonitem = new JsonItem(new Value_AssociativeArray());

                            //値型。
                            System.Type t_list_value_type = ReflectionTool.ReflectionTool.GetListValueType(a_from_object.GetType());

                            if (t_list_value_type == typeof(System.Object))
                            {
                                //ワークに追加。
                                foreach (System.Collections.DictionaryEntry t_from_pair in t_from_dictionary)
                                {
                                    string t_from_listitem_key_string = (string)t_from_pair.Key;
                                    if (t_from_listitem_key_string != null)
                                    {
                                        System.Object t_from_listitem_object = t_from_pair.Value;
                                        a_workpool.Add(WorkPool.ModeAddAssociativeArray.Start, t_to_jsonitem, t_from_listitem_key_string, t_from_listitem_object, t_from_listitem_object.GetType(), a_from_option, a_nest + 1);
                                    }
                                    else
                                    {
                                        //NULL処理。
                                        //keyがnullの場合は追加しない。
                                    }
                                }
                            }
                            else
                            {
                                //ワークに追加。
                                foreach (System.Collections.DictionaryEntry t_from_pair in t_from_dictionary)
                                {
                                    string t_from_listitem_key_string = (string)t_from_pair.Key;
                                    if (t_from_listitem_key_string != null)
                                    {
                                        System.Object t_from_listitem_object = t_from_pair.Value;
                                        a_workpool.Add(WorkPool.ModeAddAssociativeArray.Start, t_to_jsonitem, t_from_listitem_key_string, t_from_listitem_object, t_list_value_type, a_from_option, a_nest + 1);
                                    }
                                    else
                                    {
                                        //NULL処理。
                                        //keyがnullの場合は追加しない。
                                    }
                                }
                            }

                            //成功。
                            return(t_to_jsonitem);
                        }
                        else
                        {
                            //Generic.Dictionary<key != string.>

                            JsonItem t_to_jsonitem = new JsonItem(new Value_IndexArray());

                            //サイズがわかるので要素確保。
                            t_to_jsonitem.ReSize(t_from_dictionary.Count);

                            int t_index = 0;

                            //値型。
                            System.Type t_list_value_type = ReflectionTool.ReflectionTool.GetListValueType(a_from_type);

                            if (t_list_value_type == typeof(System.Object))
                            {
                                if (t_list_key_type == typeof(System.Object))
                                {
                                    //ワークに追加。
                                    foreach (System.Collections.DictionaryEntry t_from_listitem in t_from_dictionary)
                                    {
                                        JsonItem t_keyvalue_jsonitem = new JsonItem(new Value_AssociativeArray());
                                        t_to_jsonitem.SetItem(t_index, t_keyvalue_jsonitem, false);
                                        a_workpool.Add(WorkPool.ModeAddAssociativeArray.Start, t_keyvalue_jsonitem, "KEY", t_from_listitem.Key, t_from_listitem.Key.GetType(), a_from_option, a_nest + 1);
                                        a_workpool.Add(WorkPool.ModeAddAssociativeArray.Start, t_keyvalue_jsonitem, "VALUE", t_from_listitem.Value, t_from_listitem.Value.GetType(), a_from_option, a_nest + 1);
                                        t_index++;
                                    }
                                }
                                else
                                {
                                    //ワークに追加。
                                    foreach (System.Collections.DictionaryEntry t_from_listitem in t_from_dictionary)
                                    {
                                        JsonItem t_keyvalue_jsonitem = new JsonItem(new Value_AssociativeArray());
                                        t_to_jsonitem.SetItem(t_index, t_keyvalue_jsonitem, false);
                                        a_workpool.Add(WorkPool.ModeAddAssociativeArray.Start, t_keyvalue_jsonitem, "KEY", t_from_listitem.Key, t_list_key_type, a_from_option, a_nest + 1);
                                        a_workpool.Add(WorkPool.ModeAddAssociativeArray.Start, t_keyvalue_jsonitem, "VALUE", t_from_listitem.Value, t_from_listitem.Value.GetType(), a_from_option, a_nest + 1);
                                        t_index++;
                                    }
                                }
                            }
                            else
                            {
                                if (t_list_key_type == typeof(System.Object))
                                {
                                    //ワークに追加。
                                    foreach (System.Collections.DictionaryEntry t_from_listitem in t_from_dictionary)
                                    {
                                        JsonItem t_keyvalue_jsonitem = new JsonItem(new Value_AssociativeArray());
                                        t_to_jsonitem.SetItem(t_index, t_keyvalue_jsonitem, false);
                                        a_workpool.Add(WorkPool.ModeAddAssociativeArray.Start, t_keyvalue_jsonitem, "KEY", t_from_listitem.Key, t_from_listitem.Key.GetType(), a_from_option, a_nest + 1);
                                        a_workpool.Add(WorkPool.ModeAddAssociativeArray.Start, t_keyvalue_jsonitem, "VALUE", t_from_listitem.Value, t_list_value_type, a_from_option, a_nest + 1);
                                        t_index++;
                                    }
                                }
                                else
                                {
                                    //ワークに追加。
                                    foreach (System.Collections.DictionaryEntry t_from_listitem in t_from_dictionary)
                                    {
                                        JsonItem t_keyvalue_jsonitem = new JsonItem(new Value_AssociativeArray());
                                        t_to_jsonitem.SetItem(t_index, t_keyvalue_jsonitem, false);
                                        a_workpool.Add(WorkPool.ModeAddAssociativeArray.Start, t_keyvalue_jsonitem, "KEY", t_from_listitem.Key, t_list_key_type, a_from_option, a_nest + 1);
                                        a_workpool.Add(WorkPool.ModeAddAssociativeArray.Start, t_keyvalue_jsonitem, "VALUE", t_from_listitem.Value, t_list_value_type, a_from_option, a_nest + 1);
                                        t_index++;
                                    }
                                }
                            }

                            //成功。
                            return(t_to_jsonitem);
                        }
                    }
                }

                //ICollection
                {
                    System.Collections.ICollection t_from_collection = a_from_object as System.Collections.ICollection;
                    if (t_from_collection != null)
                    {
                        //Generic.List
                        //Generic.Stack
                        //Generic.LinkedList
                        //Generic.Queue
                        //Generic.SortedSet

                        JsonItem t_to_jsonitem = new JsonItem(new Value_IndexArray());

                        //サイズがわかるので要素確保。
                        t_to_jsonitem.ReSize(t_from_collection.Count);

                        int t_index = 0;

                        //値型。
                        System.Type t_list_value_type = ReflectionTool.ReflectionTool.GetListValueType(a_from_type);

                        if (t_list_value_type == typeof(System.Object))
                        {
                            //Collections.ArrayList

                            //ワークに追加。
                            foreach (System.Object t_from_listitem in t_from_collection)
                            {
                                a_workpool.Add(WorkPool.ModeSetIndexArray.Start, t_to_jsonitem, t_index, t_from_listitem, t_from_listitem.GetType(), a_from_option, a_nest + 1);
                                t_index++;
                            }
                        }
                        else
                        {
                            //ワークに追加。
                            foreach (System.Object t_from_listitem in t_from_collection)
                            {
                                a_workpool.Add(WorkPool.ModeSetIndexArray.Start, t_to_jsonitem, t_index, t_from_listitem, t_list_value_type, a_from_option, a_nest + 1);
                                t_index++;
                            }
                        }

                        //成功。
                        return(t_to_jsonitem);
                    }
                }

                //IEnumerable
                {
                    System.Collections.IEnumerable t_from_enumerable = a_from_object as System.Collections.IEnumerable;
                    if (t_from_enumerable != null)
                    {
                        //Generic.HashSet

                        JsonItem t_to_jsonitem = new JsonItem(new Value_IndexArray());

                        //値型。
                        System.Type t_list_value_type = ReflectionTool.ReflectionTool.GetListValueType(a_from_type);

                        if (t_list_value_type == typeof(System.Object))
                        {
                            //ワークに追加。
                            foreach (System.Object t_from_listitem in t_from_enumerable)
                            {
                                a_workpool.Add(WorkPool.ModeAddIndexArray.Start, t_to_jsonitem, t_from_listitem, t_from_listitem.GetType(), a_from_option, a_nest + 1);
                            }
                        }
                        else
                        {
                            //ワークに追加。
                            foreach (System.Object t_from_listitem in t_from_enumerable)
                            {
                                a_workpool.Add(WorkPool.ModeAddIndexArray.Start, t_to_jsonitem, t_from_listitem, t_list_value_type, a_from_option, a_nest + 1);
                            }
                        }

                        //成功。
                        return(t_to_jsonitem);
                    }
                }

                //class,struct
                {
                    JsonItem t_to_jsonitem = new JsonItem(new Value_AssociativeArray());

                    //メンバーリスト。取得。
                    System.Collections.Generic.List <System.Reflection.FieldInfo> t_fieldinfo_list = new System.Collections.Generic.List <System.Reflection.FieldInfo>();
                    ConvertTool.GetMemberListAll(a_from_type, t_fieldinfo_list);

                    //ワークに追加。
                    foreach (System.Reflection.FieldInfo t_fieldinfo in t_fieldinfo_list)
                    {
                        a_workpool.Add(WorkPool.ModeFieldInfo.Start, t_to_jsonitem, t_fieldinfo, a_from_object, a_nest + 1);
                    }

                    //成功。
                    return(t_to_jsonitem);
                }
            }
        }
Пример #8
0
        /** Convert
         */
        public static JsonItem Convert(System.Object a_from_object, System.Type a_from_type, ConvertToJsonItemOption a_from_option, WorkPool a_workpool, int a_nest)
        {
            {
                //[]

                System.Array t_array_raw = (System.Array)a_from_object;

                JsonItem t_to_jsonitem = new JsonItem(new Value_IndexArray());

                //サイズ確保。
                t_to_jsonitem.ReSize(t_array_raw.Length);

                //値型。
                System.Type t_list_value_type = ReflectionTool.ReflectionTool.GetListValueType(a_from_type);

                if (t_list_value_type == typeof(System.Object))
                {
                    for (int ii = 0; ii < t_array_raw.Length; ii++)
                    {
                        //ワークに追加。
                        System.Object t_listitem_object = t_array_raw.GetValue(ii);
                        a_workpool.Add(WorkPool.ModeSetIndexArray.Start, t_to_jsonitem, ii, t_listitem_object, t_listitem_object.GetType(), a_from_option, a_nest + 1);
                    }
                }
                else
                {
                    for (int ii = 0; ii < t_array_raw.Length; ii++)
                    {
                        //ワークに追加。
                        System.Object t_listitem_object = t_array_raw.GetValue(ii);
                        a_workpool.Add(WorkPool.ModeSetIndexArray.Start, t_to_jsonitem, ii, t_listitem_object, t_list_value_type, a_from_option, a_nest + 1);
                    }
                }

                //成功。
                return(t_to_jsonitem);
            }
        }
        /** Convert
         */
        public static JsonItem Convert(System.Object a_from_object, ConvertToJsonItemOption a_from_option)
        {
            if ((a_from_option & ConvertToJsonItemOption.EnumString) > 0)
            {
                //enumの文字列化。

                string t_value = a_from_object.ToString();

                if (t_value != null)
                {
                    return(new JsonItem(new Value_StringData(t_value)));
                }
                else
                {
                    //NULL処理。
                    return(null);
                }
            }
            else
            {
                //enumの数値化。

                System.Enum t_enum = (System.Enum)a_from_object;
                if (t_enum != null)
                {
                    switch (t_enum.GetTypeCode())
                    {
                    case System.TypeCode.Byte:
                    {
                        return(new JsonItem(new Value_Number <System.Byte>((System.Byte)a_from_object)));
                    } break;

                    case System.TypeCode.SByte:
                    {
                        return(new JsonItem(new Value_Number <System.SByte>((System.SByte)a_from_object)));
                    } break;

                    case System.TypeCode.Int16:
                    {
                        return(new JsonItem(new Value_Number <System.Int16>((System.Int16)a_from_object)));
                    } break;

                    case System.TypeCode.UInt16:
                    {
                        return(new JsonItem(new Value_Number <System.UInt16>((System.UInt16)a_from_object)));
                    } break;

                    case System.TypeCode.Int32:
                    {
                        return(new JsonItem(new Value_Number <System.Int32>((System.Int32)a_from_object)));
                    } break;

                    case System.TypeCode.UInt32:
                    {
                        return(new JsonItem(new Value_Number <System.UInt32>((System.UInt32)a_from_object)));
                    } break;

                    case System.TypeCode.Int64:
                    {
                        return(new JsonItem(new Value_Number <System.Int64>((System.Int64)a_from_object)));
                    } break;

                    case System.TypeCode.UInt64:
                    {
                        return(new JsonItem(new Value_Number <System.UInt64>((System.UInt64)a_from_object)));
                    } break;

                    default:
                    {
                        Tool.Assert(false);
                    } break;
                    }
                }
            }

            //失敗。
            Tool.Assert(false);
            return(null);
        }
        /** Convert
         */
        public static JsonItem Convert(System.Object a_from_object, System.Type a_from_type, ConvertToJsonItemOption a_from_option, ObjectToJsonItem_WorkPool a_workpool, int a_nest)
        {
            try{
                //[]

                System.Array t_array_raw = (System.Array)a_from_object;

                JsonItem t_to_jsonitem = new JsonItem(new Value_IndexArray());

                //サイズ確保。
                t_to_jsonitem.ReSize(t_array_raw.Length);

                //値型。
                System.Type t_list_value_type = Fee.ReflectionTool.Utility.GetListValueType(a_from_type);

                if (t_list_value_type == typeof(System.Object))
                {
                    for (int ii = 0; ii < t_array_raw.Length; ii++)
                    {
                        //ワークに追加。
                        System.Object t_listitem_object = t_array_raw.GetValue(ii);
                        a_workpool.Add(ObjectToJsonItem_WorkPool.ModeSetIndexArray.Start, t_to_jsonitem, ii, t_listitem_object, t_listitem_object.GetType(), a_from_option, a_nest + 1);
                    }
                }
                else
                {
                    for (int ii = 0; ii < t_array_raw.Length; ii++)
                    {
                        //ワークに追加。
                        System.Object t_listitem_object = t_array_raw.GetValue(ii);
                        a_workpool.Add(ObjectToJsonItem_WorkPool.ModeSetIndexArray.Start, t_to_jsonitem, ii, t_listitem_object, t_list_value_type, a_from_option, a_nest + 1);
                    }
                }

                //成功。
                return(t_to_jsonitem);
            }catch (System.Exception t_exception) {
                Tool.DebugReThrow(t_exception);
            }

            //失敗。
            Tool.Assert(false);
            return(null);
        }