private static void Init()
        {
            var window = EditorWindow.GetWindow <QuestionEditorHelperWindow>();

            jsonAdapter = new UnityJsonAdapter();
            window.Show();
        }
Esempio n. 2
0
        static bool TryDeserializeAdapter <TValue>(IJsonAdapter adapter, UnsafeValueView view, ref TValue value, List <DeserializationEvent> events)
        {
            if (adapter is IJsonAdapter <TValue> typed)
            {
                try
                {
                    value = typed.Deserialize(view.AsSafe());
                }
                catch (Exception e)
                {
                    events.Add(new DeserializationEvent(EventType.Exception, e));
                }
                return(true);
            }

            if (adapter is Adapters.Contravariant.IJsonAdapter <TValue> typedContravariant)
            {
                try
                {
                    // @TODO Type checking on return value.
                    value = (TValue)typedContravariant.Deserialize(view.AsSafe());
                }
                catch (Exception e)
                {
                    events.Add(new DeserializationEvent(EventType.Exception, e));
                }
                return(true);
            }

            return(false);
        }
        static bool TrySerializeAdapter <TValue>(IJsonAdapter adapter, JsonWriter writer, ref TValue value)
        {
            if (adapter is IJsonAdapter <TValue> typed)
            {
                using (var buffer = new JsonStringBuffer(16, Allocator.TempJob))
                {
                    typed.Serialize(buffer, value);

                    unsafe
                    {
                        writer.AsUnsafe().WriteValueLiteral(buffer.GetUnsafePtr(), buffer.Length);
                    }
                }
                return(true);
            }

            if (adapter is Adapters.Contravariant.IJsonAdapter <TValue> typedContravariant)
            {
                using (var buffer = new JsonStringBuffer(16, Allocator.TempJob))
                {
                    typedContravariant.Serialize(buffer, value);

                    unsafe
                    {
                        writer.AsUnsafe().WriteValueLiteral(buffer.GetUnsafePtr(), buffer.Length);
                    }
                }
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Adds the specified <see cref="IJsonAdapter"/> to the set of global adapters. This is be included by default in all JsonSerialization calls.
        /// </summary>
        /// <param name="adapter">The adapter to add.</param>
        /// <exception cref="ArgumentException">The given adapter is already registered.</exception>
        public static void AddGlobalAdapter(IJsonAdapter adapter)
        {
            if (s_Adapters.Contains(adapter))
            {
                throw new ArgumentException("IJsonAdapter has already been registered.");
            }

            s_Adapters.Add(adapter);
        }
        /// <summary>
        /// Removes the specified <see cref="IJsonAdapter"/> from the set of global adapters.
        /// </summary>
        /// <param name="adapter">The adapter to remove.</param>
        /// <exception cref="ArgumentException">The given adapter has not been registered.</exception>
        public static void RemoveGlobalAdapter(IJsonAdapter adapter)
        {
            if (!s_Adapters.Contains(adapter))
            {
                throw new ArgumentException("IJsonAdapter has not been registered.");
            }

            s_Adapters.Remove(adapter);
        }
 public Enumerator(List <IJsonAdapter> user, List <IJsonAdapter> global, JsonAdapter @internal)
 {
     m_User     = user;
     m_Global   = global;
     m_Internal = @internal;
     m_Current  = null;
     m_State    = null != user ? State.User : null != global ? State.Global : State.Internal;
     m_Index    = -1;
 }
        private void OnQnABundlesTab()
        {
            GUILayout.Label(new GUIContent("Generate QnA Bundle"));

            category       = (QuestionCategory)EditorGUILayout.EnumPopup(new GUIContent("Select Category"), category);
            questionsCount = EditorGUILayout.IntField(new GUIContent("Question Fields To Add"), questionsCount);

            if (GUILayout.Button(new GUIContent("Generate")))
            {
                string dirPath = string.Concat(Paths.RESOURCES_PATH, "/", Paths.DEFAULT_QnABUNDLES_PATH);

                if (!Directory.Exists(string.Concat(Application.dataPath, "/", dirPath)))
                {
                    Assets.CreateFolder(dirPath);
                }

                var filePath = string.Concat(Application.dataPath, "/", dirPath, "/", category.ToString(), "_Bundle.json");


                if (File.Exists(filePath))
                {
                    Debug.Log("Bundle with this category already exists, please edit it directly to add more questions!");
                    return;
                }

                using (var sr = File.CreateText(filePath))
                {
                    if (jsonAdapter == null)
                    {
                        jsonAdapter = new UnityJsonAdapter();
                    }

                    var bundle = new QnADataBundle()
                    {
                        category = category, QnADatas = new QnAData[questionsCount]
                    };
                    for (int i = 0; i < questionsCount; i++)
                    {
                        bundle.QnADatas[i] = QnAData.GetEmpty();
                    }

                    var json = jsonAdapter.Serialize(bundle);

                    sr.Write(json);
                    AssetDatabase.Refresh();

                    Debug.LogFormat("Bundle successfuly created at {0}", filePath);
                }
            }
        }
Esempio n. 8
0
        static bool TrySerializeAdapter <TValue>(IJsonAdapter adapter, JsonStringBuffer writer, ref TValue value)
        {
            if (adapter is IJsonAdapter <TValue> typed)
            {
                typed.Serialize(writer, value);
                return(true);
            }

            if (adapter is Adapters.Contravariant.IJsonAdapter <TValue> typedContravariant)
            {
                typedContravariant.Serialize(writer, value);
                return(true);
            }

            return(false);
        }
            public bool MoveNext()
            {
                for (;;)
                {
                    m_Index++;

                    switch (m_State)
                    {
                    case State.User:
                        if (m_Index < m_User.Count)
                        {
                            m_Current = m_User[m_Index];
                            return(true);
                        }
                        m_State = State.Global;
                        m_Index = -1;
                        break;

                    case State.Global:
                        if (m_Index < m_Global.Count)
                        {
                            m_Current = m_Global[m_Index];
                            return(true);
                        }
                        m_State = State.Internal;
                        m_Index = -1;
                        break;

                    case State.Internal:
                        m_Current = m_Internal;
                        m_State   = State.End;
                        m_Index   = -1;
                        return(true);

                    case State.End:
                        m_Index = -1;
                        return(false);

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }
Esempio n. 10
0
 public BooksRepository(IJsonAdapter adapter)
     : base(adapter)
 {
 }
Esempio n. 11
0
 public LocalQuestionLoader(IJsonAdapter jsonAdapter, string questionBundlesPath = Paths.DEFAULT_QnABUNDLES_PATH)
 {
     this.jsonAdapter = jsonAdapter;
     bundlesPath      = questionBundlesPath;
 }
Esempio n. 12
0
        public JsonRepository(IJsonAdapter context)
        {
            context.NullGuard();

            _context = context;
        }
Esempio n. 13
0
 public GenreRepository(IJsonAdapter adapter)
     : base(adapter)
 {
 }
Esempio n. 14
0
 public Json(IJsonAdapter adapter)
 {
     jsonParse = adapter;
 }
Esempio n. 15
0
 /// <summary>
 /// 构建一个json工具
 /// </summary>
 /// <param name="adapter">适配器</param>
 public Json(IJsonAdapter adapter)
 {
     this.adapter = adapter;
 }
Esempio n. 16
0
 public IConfigurator JsonAdapter(IJsonAdapter adapter)
 {
     Configuration.JsonAdapter = adapter;
     return(this);
 }
Esempio n. 17
0
 public AuthorRepository(IJsonAdapter adapter)
     : base(adapter)
 {
 }