コード例 #1
0
ファイル: ServerType.cs プロジェクト: whztt07/battleofmages
    // Reader
    public static object JsonDeserializer(Jboy.JsonReader reader)
    {
        reader.TryReadPropertyName("serverType");
        switch (reader.ReadString())
        {
        case "Arena":
            return(ServerType.Arena);

        case "FFA":
            return(ServerType.FFA);

        case "Town":
            return(ServerType.Town);

        case "World":
            return(ServerType.World);

        default:
            throw new System.ArgumentException("Unknown server type");
        }
    }
コード例 #2
0
    // Read a new class instance from JSON
    public static T ReadJSONClassInstance <T>(Jboy.JsonReader reader) where T : new()
    {
        T instance = new T();

        reader.ReadObjectStart();

        string propName;
        bool   success  = true;
        var    typeInfo = typeof(T);

        while (true)
        {
            success = reader.TryReadPropertyName(out propName);

            if (success)
            {
                var field = typeInfo.GetField(propName);

                if (field == null)
                {
                    LogManager.DB.LogError("Field does not exist: '" + propName + "'");
                    continue;
                }

                if (!field.IsStatic)
                {
                    field.SetValue(instance, GenericSerializer.ReadJSONValue(reader, field));
                }
            }
            else
            {
                break;
            }
        }

        reader.ReadObjectEnd();

        return(instance);
    }