Skip to content

EVE SDE Schema

Documentation for third-party developers

typeDogma.jsonl

Schema

  • _key (required): integer
    Range: 18 .. 89782
  • dogmaAttributes (required): array of object

    • attributeID (required): integer
      Range: 3 .. 5964
    • value (required): number
      Range: -900000000000 .. 8640000000000
  • dogmaEffects: array of object

    • effectID (required): integer
      Range: 4 .. 12579
    • isDefault (required): boolean

Code snippets

// <auto-generated />
//
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
//    using QuickType;
//
//    var typeDogma = TypeDogma.FromJson(jsonString);

namespace QuickType
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class TypeDogma
    {
        [JsonProperty("_key")]
        public long Key { get; set; }

        [JsonProperty("dogmaAttributes")]
        public DogmaAttribute[] DogmaAttributes { get; set; }

        [JsonProperty("dogmaEffects", NullValueHandling = NullValueHandling.Ignore)]
        public DogmaEffect[] DogmaEffects { get; set; }
    }

    public partial class DogmaAttribute
    {
        [JsonProperty("attributeID")]
        public long AttributeId { get; set; }

        [JsonProperty("value")]
        [JsonConverter(typeof(MinMaxValueCheckConverter))]
        public double Value { get; set; }
    }

    public partial class DogmaEffect
    {
        [JsonProperty("effectID")]
        public long EffectId { get; set; }

        [JsonProperty("isDefault")]
        public bool IsDefault { get; set; }
    }

    public partial class TypeDogma
    {
        public static TypeDogma FromJson(string json) => JsonConvert.DeserializeObject<TypeDogma>(json, QuickType.Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this TypeDogma self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
    }

    internal static class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
            Converters =
            {
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
        };
    }

    internal class MinMaxValueCheckConverter : JsonConverter
    {
        public override bool CanConvert(Type t) => t == typeof(double) || t == typeof(double?);

        public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null) return null;
            var value = serializer.Deserialize<double>(reader);
            if (value >= -900000000000 && value <= 8640000000000)
            {
                return value;
            }
            throw new Exception("Cannot unmarshal type double");
        }

        public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
        {
            if (untypedValue == null)
            {
                serializer.Serialize(writer, null);
                return;
            }
            var value = (double)untypedValue;
            if (value >= -900000000000 && value <= 8640000000000)
            {
                serializer.Serialize(writer, value);
                return;
            }
            throw new Exception("Cannot marshal type double");
        }

        public static readonly MinMaxValueCheckConverter Singleton = new MinMaxValueCheckConverter();
    }
}
// Code generated from JSON Schema using quicktype. DO NOT EDIT.
// To parse and unparse this JSON data, add this code to your project and do:
//
//    typeDogma, err := UnmarshalTypeDogma(bytes)
//    bytes, err = typeDogma.Marshal()

package model

import "encoding/json"

func UnmarshalTypeDogma(data []byte) (TypeDogma, error) {
    var r TypeDogma
    err := json.Unmarshal(data, &r)
    return r, err
}

func (r *TypeDogma) Marshal() ([]byte, error) {
    return json.Marshal(r)
}

type TypeDogma struct {
    Key             int64            `json:"_key"`
    DogmaAttributes []DogmaAttribute `json:"dogmaAttributes"`
    DogmaEffects    []DogmaEffect    `json:"dogmaEffects,omitempty"`
}

type DogmaAttribute struct {
    AttributeID int64   `json:"attributeID"`
    Value       float64 `json:"value"`
}

type DogmaEffect struct {
    EffectID  int64 `json:"effectID"`
    IsDefault bool  `json:"isDefault"`
}
{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
        "_key": {
            "type": "integer",
            "minimum": 18,
            "maximum": 89782
        },
        "dogmaAttributes": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "attributeID": {
                        "type": "integer",
                        "minimum": 3,
                        "maximum": 5964
                    },
                    "value": {
                        "type": "number",
                        "minimum": -9.0E11,
                        "maximum": 8.64E12
                    }
                },
                "required": [
                    "attributeID",
                    "value"
                ]
            },
            "minItems": 1,
            "maxItems": 134
        },
        "dogmaEffects": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "effectID": {
                        "type": "integer",
                        "minimum": 4,
                        "maximum": 12579
                    },
                    "isDefault": {
                        "type": "boolean"
                    }
                },
                "required": [
                    "effectID",
                    "isDefault"
                ]
            },
            "minItems": 1,
            "maxItems": 26
        }
    },
    "required": [
        "_key",
        "dogmaAttributes"
    ]
}
// To parse the JSON, install kotlin's serialization plugin and do:
//
// val json      = Json { allowStructuredMapKeys = true }
// val typeDogma = json.parse(TypeDogma.serializer(), jsonString)

package model

import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*

@Serializable
data class TypeDogma (
    @SerialName("_key")
    val key: Long,

    val dogmaAttributes: List<DogmaAttribute>,
    val dogmaEffects: List<DogmaEffect>? = null
)

@Serializable
data class DogmaAttribute (
    @SerialName("attributeID")
    val attributeId: Long,

    val value: Double
)

@Serializable
data class DogmaEffect (
    @SerialName("effectID")
    val effectId: Long,

    val isDefault: Boolean
)
<?php

// This is a autogenerated file:TypeDogma

class TypeDogma {
    private int $key; // json:_key Required
    private array $dogmaAttributes; // json:dogmaAttributes Required
    private ?array $dogmaEffects; // json:dogmaEffects Optional

    /**
     * @param int $key
     * @param array $dogmaAttributes
     * @param array|null $dogmaEffects
     */
    public function __construct(int $key, array $dogmaAttributes, ?array $dogmaEffects) {
        $this->key = $key;
        $this->dogmaAttributes = $dogmaAttributes;
        $this->dogmaEffects = $dogmaEffects;
    }

    /**
     * @param int $value
     * @throws Exception
     * @return int
     */
    public static function fromKey(int $value): int {
        return $value; /*int*/
    }

    /**
     * @throws Exception
     * @return int
     */
    public function toKey(): int {
        if (TypeDogma::validateKey($this->key))  {
            return $this->key; /*int*/
        }
        throw new Exception('never get to this TypeDogma::key');
    }

    /**
     * @param int
     * @return bool
     * @throws Exception
     */
    public static function validateKey(int $value): bool {
        if (!is_integer($value)) {
            throw new Exception("Attribute Error:TypeDogma::key");
        }
        return true;
    }

    /**
     * @throws Exception
     * @return int
     */
    public function getKey(): int {
        if (TypeDogma::validateKey($this->key))  {
            return $this->key;
        }
        throw new Exception('never get to getKey TypeDogma::key');
    }

    /**
     * @return int
     */
    public static function sampleKey(): int {
        return 31; /*31:key*/
    }

    /**
     * @param array $value
     * @throws Exception
     * @return array
     */
    public static function fromDogmaAttributes(array $value): array {
        return  array_map(function ($value) {
            return DogmaAttribute::from($value); /*class*/
        }, $value);
    }

    /**
     * @throws Exception
     * @return array
     */
    public function toDogmaAttributes(): array {
        if (TypeDogma::validateDogmaAttributes($this->dogmaAttributes))  {
            return array_map(function ($value) {
                return $value->to(); /*class*/
            }, $this->dogmaAttributes);
        }
        throw new Exception('never get to this TypeDogma::dogmaAttributes');
    }

    /**
     * @param array
     * @return bool
     * @throws Exception
     */
    public static function validateDogmaAttributes(array $value): bool {
        if (!is_array($value)) {
            throw new Exception("Attribute Error:TypeDogma::dogmaAttributes");
        }
        array_walk($value, function($value_v) {
            $value_v->validate();
        });
        return true;
    }

    /**
     * @throws Exception
     * @return array
     */
    public function getDogmaAttributes(): array {
        if (TypeDogma::validateDogmaAttributes($this->dogmaAttributes))  {
            return $this->dogmaAttributes;
        }
        throw new Exception('never get to getDogmaAttributes TypeDogma::dogmaAttributes');
    }

    /**
     * @return array
     */
    public static function sampleDogmaAttributes(): array {
        return  array(
            DogmaAttribute::sample() /*32:*/
        ); /* 32:dogmaAttributes*/
    }

    /**
     * @param ?array $value
     * @throws Exception
     * @return ?array
     */
    public static function fromDogmaEffects(?array $value): ?array {
        if (!is_null($value)) {
            return  array_map(function ($value) {
                return DogmaEffect::from($value); /*class*/
            }, $value);
        } else {
            return null;
        }
    }

    /**
     * @throws Exception
     * @return ?array
     */
    public function toDogmaEffects(): ?array {
        if (TypeDogma::validateDogmaEffects($this->dogmaEffects))  {
            if (!is_null($this->dogmaEffects)) {
                return array_map(function ($value) {
                    return $value->to(); /*class*/
                }, $this->dogmaEffects);
            } else {
                return  null;
            }
        }
        throw new Exception('never get to this TypeDogma::dogmaEffects');
    }

    /**
     * @param array|null
     * @return bool
     * @throws Exception
     */
    public static function validateDogmaEffects(?array $value): bool {
        if (!is_null($value)) {
            if (!is_array($value)) {
                throw new Exception("Attribute Error:TypeDogma::dogmaEffects");
            }
            array_walk($value, function($value_v) {
                $value_v->validate();
            });
        }
        return true;
    }

    /**
     * @throws Exception
     * @return ?array
     */
    public function getDogmaEffects(): ?array {
        if (TypeDogma::validateDogmaEffects($this->dogmaEffects))  {
            return $this->dogmaEffects;
        }
        throw new Exception('never get to getDogmaEffects TypeDogma::dogmaEffects');
    }

    /**
     * @return ?array
     */
    public static function sampleDogmaEffects(): ?array {
        return  array(
            DogmaEffect::sample() /*33:*/
        ); /* 33:dogmaEffects*/
    }

    /**
     * @throws Exception
     * @return bool
     */
    public function validate(): bool {
        return TypeDogma::validateKey($this->key)
        || TypeDogma::validateDogmaAttributes($this->dogmaAttributes)
        || TypeDogma::validateDogmaEffects($this->dogmaEffects);
    }

    /**
     * @return stdClass
     * @throws Exception
     */
    public function to(): stdClass  {
        $out = new stdClass();
        $out->{'_key'} = $this->toKey();
        $out->{'dogmaAttributes'} = $this->toDogmaAttributes();
        $out->{'dogmaEffects'} = $this->toDogmaEffects();
        return $out;
    }

    /**
     * @param stdClass $obj
     * @return TypeDogma
     * @throws Exception
     */
    public static function from(stdClass $obj): TypeDogma {
        return new TypeDogma(
         TypeDogma::fromKey($obj->{'_key'})
        ,TypeDogma::fromDogmaAttributes($obj->{'dogmaAttributes'})
        ,TypeDogma::fromDogmaEffects($obj->{'dogmaEffects'})
        );
    }

    /**
     * @return TypeDogma
     */
    public static function sample(): TypeDogma {
        return new TypeDogma(
         TypeDogma::sampleKey()
        ,TypeDogma::sampleDogmaAttributes()
        ,TypeDogma::sampleDogmaEffects()
        );
    }
}

// This is a autogenerated file:DogmaAttribute

class DogmaAttribute {
    private int $attributeId; // json:attributeID Required
    private float $value; // json:value Required

    /**
     * @param int $attributeId
     * @param float $value
     */
    public function __construct(int $attributeId, float $value) {
        $this->attributeId = $attributeId;
        $this->value = $value;
    }

    /**
     * @param int $value
     * @throws Exception
     * @return int
     */
    public static function fromAttributeId(int $value): int {
        return $value; /*int*/
    }

    /**
     * @throws Exception
     * @return int
     */
    public function toAttributeId(): int {
        if (DogmaAttribute::validateAttributeId($this->attributeId))  {
            return $this->attributeId; /*int*/
        }
        throw new Exception('never get to this DogmaAttribute::attributeId');
    }

    /**
     * @param int
     * @return bool
     * @throws Exception
     */
    public static function validateAttributeId(int $value): bool {
        if (!is_integer($value)) {
            throw new Exception("Attribute Error:DogmaAttribute::attributeId");
        }
        return true;
    }

    /**
     * @throws Exception
     * @return int
     */
    public function getAttributeId(): int {
        if (DogmaAttribute::validateAttributeId($this->attributeId))  {
            return $this->attributeId;
        }
        throw new Exception('never get to getAttributeId DogmaAttribute::attributeId');
    }

    /**
     * @return int
     */
    public static function sampleAttributeId(): int {
        return 31; /*31:attributeId*/
    }

    /**
     * @param float $value
     * @throws Exception
     * @return float
     */
    public static function fromValue(float $value): float {
        return $value; /*float*/
    }

    /**
     * @throws Exception
     * @return float
     */
    public function toValue(): float {
        if (DogmaAttribute::validateValue($this->value))  {
            return $this->value; /*float*/
        }
        throw new Exception('never get to this DogmaAttribute::value');
    }

    /**
     * @param float
     * @return bool
     * @throws Exception
     */
    public static function validateValue(float $value): bool {
        if (!is_float($value)) {
            throw new Exception("Attribute Error:DogmaAttribute::value");
        }
        return true;
    }

    /**
     * @throws Exception
     * @return float
     */
    public function getValue(): float {
        if (DogmaAttribute::validateValue($this->value))  {
            return $this->value;
        }
        throw new Exception('never get to getValue DogmaAttribute::value');
    }

    /**
     * @return float
     */
    public static function sampleValue(): float {
        return 32.032; /*32:value*/
    }

    /**
     * @throws Exception
     * @return bool
     */
    public function validate(): bool {
        return DogmaAttribute::validateAttributeId($this->attributeId)
        || DogmaAttribute::validateValue($this->value);
    }

    /**
     * @return stdClass
     * @throws Exception
     */
    public function to(): stdClass  {
        $out = new stdClass();
        $out->{'attributeID'} = $this->toAttributeId();
        $out->{'value'} = $this->toValue();
        return $out;
    }

    /**
     * @param stdClass $obj
     * @return DogmaAttribute
     * @throws Exception
     */
    public static function from(stdClass $obj): DogmaAttribute {
        return new DogmaAttribute(
         DogmaAttribute::fromAttributeId($obj->{'attributeID'})
        ,DogmaAttribute::fromValue($obj->{'value'})
        );
    }

    /**
     * @return DogmaAttribute
     */
    public static function sample(): DogmaAttribute {
        return new DogmaAttribute(
         DogmaAttribute::sampleAttributeId()
        ,DogmaAttribute::sampleValue()
        );
    }
}

// This is a autogenerated file:DogmaEffect

class DogmaEffect {
    private int $effectId; // json:effectID Required
    private bool $isDefault; // json:isDefault Required

    /**
     * @param int $effectId
     * @param bool $isDefault
     */
    public function __construct(int $effectId, bool $isDefault) {
        $this->effectId = $effectId;
        $this->isDefault = $isDefault;
    }

    /**
     * @param int $value
     * @throws Exception
     * @return int
     */
    public static function fromEffectId(int $value): int {
        return $value; /*int*/
    }

    /**
     * @throws Exception
     * @return int
     */
    public function toEffectId(): int {
        if (DogmaEffect::validateEffectId($this->effectId))  {
            return $this->effectId; /*int*/
        }
        throw new Exception('never get to this DogmaEffect::effectId');
    }

    /**
     * @param int
     * @return bool
     * @throws Exception
     */
    public static function validateEffectId(int $value): bool {
        if (!is_integer($value)) {
            throw new Exception("Attribute Error:DogmaEffect::effectId");
        }
        return true;
    }

    /**
     * @throws Exception
     * @return int
     */
    public function getEffectId(): int {
        if (DogmaEffect::validateEffectId($this->effectId))  {
            return $this->effectId;
        }
        throw new Exception('never get to getEffectId DogmaEffect::effectId');
    }

    /**
     * @return int
     */
    public static function sampleEffectId(): int {
        return 31; /*31:effectId*/
    }

    /**
     * @param bool $value
     * @throws Exception
     * @return bool
     */
    public static function fromIsDefault(bool $value): bool {
        return $value; /*bool*/
    }

    /**
     * @throws Exception
     * @return bool
     */
    public function toIsDefault(): bool {
        if (DogmaEffect::validateIsDefault($this->isDefault))  {
            return $this->isDefault; /*bool*/
        }
        throw new Exception('never get to this DogmaEffect::isDefault');
    }

    /**
     * @param bool
     * @return bool
     * @throws Exception
     */
    public static function validateIsDefault(bool $value): bool {
        if (!is_bool($value)) {
            throw new Exception("Attribute Error:DogmaEffect::isDefault");
        }
        return true;
    }

    /**
     * @throws Exception
     * @return bool
     */
    public function getIsDefault(): bool {
        if (DogmaEffect::validateIsDefault($this->isDefault))  {
            return $this->isDefault;
        }
        throw new Exception('never get to getIsDefault DogmaEffect::isDefault');
    }

    /**
     * @return bool
     */
    public static function sampleIsDefault(): bool {
        return true; /*32:isDefault*/
    }

    /**
     * @throws Exception
     * @return bool
     */
    public function validate(): bool {
        return DogmaEffect::validateEffectId($this->effectId)
        || DogmaEffect::validateIsDefault($this->isDefault);
    }

    /**
     * @return stdClass
     * @throws Exception
     */
    public function to(): stdClass  {
        $out = new stdClass();
        $out->{'effectID'} = $this->toEffectId();
        $out->{'isDefault'} = $this->toIsDefault();
        return $out;
    }

    /**
     * @param stdClass $obj
     * @return DogmaEffect
     * @throws Exception
     */
    public static function from(stdClass $obj): DogmaEffect {
        return new DogmaEffect(
         DogmaEffect::fromEffectId($obj->{'effectID'})
        ,DogmaEffect::fromIsDefault($obj->{'isDefault'})
        );
    }

    /**
     * @return DogmaEffect
     */
    public static function sample(): DogmaEffect {
        return new DogmaEffect(
         DogmaEffect::sampleEffectId()
        ,DogmaEffect::sampleIsDefault()
        );
    }
}
from typing import Any, List, Optional, TypeVar, Callable, Type, cast


T = TypeVar("T")


def from_int(x: Any) -> int:
    assert isinstance(x, int) and not isinstance(x, bool)
    return x


def from_float(x: Any) -> float:
    assert isinstance(x, (float, int)) and not isinstance(x, bool)
    return float(x)


def to_float(x: Any) -> float:
    assert isinstance(x, (int, float))
    return x


def from_bool(x: Any) -> bool:
    assert isinstance(x, bool)
    return x


def from_list(f: Callable[[Any], T], x: Any) -> List[T]:
    assert isinstance(x, list)
    return [f(y) for y in x]


def from_none(x: Any) -> Any:
    assert x is None
    return x


def from_union(fs, x):
    for f in fs:
        try:
            return f(x)
        except:
            pass
    assert False


def to_class(c: Type[T], x: Any) -> dict:
    assert isinstance(x, c)
    return cast(Any, x).to_dict()


class DogmaAttribute:
    attribute_id: int
    value: float

    def __init__(self, attribute_id: int, value: float) -> None:
        self.attribute_id = attribute_id
        self.value = value

    @staticmethod
    def from_dict(obj: Any) -> 'DogmaAttribute':
        assert isinstance(obj, dict)
        attribute_id = from_int(obj.get("attributeID"))
        value = from_float(obj.get("value"))
        return DogmaAttribute(attribute_id, value)

    def to_dict(self) -> dict:
        result: dict = {}
        result["attributeID"] = from_int(self.attribute_id)
        result["value"] = to_float(self.value)
        return result


class DogmaEffect:
    effect_id: int
    is_default: bool

    def __init__(self, effect_id: int, is_default: bool) -> None:
        self.effect_id = effect_id
        self.is_default = is_default

    @staticmethod
    def from_dict(obj: Any) -> 'DogmaEffect':
        assert isinstance(obj, dict)
        effect_id = from_int(obj.get("effectID"))
        is_default = from_bool(obj.get("isDefault"))
        return DogmaEffect(effect_id, is_default)

    def to_dict(self) -> dict:
        result: dict = {}
        result["effectID"] = from_int(self.effect_id)
        result["isDefault"] = from_bool(self.is_default)
        return result


class TypeDogma:
    key: int
    dogma_attributes: List[DogmaAttribute]
    dogma_effects: Optional[List[DogmaEffect]]

    def __init__(self, key: int, dogma_attributes: List[DogmaAttribute], dogma_effects: Optional[List[DogmaEffect]]) -> None:
        self.key = key
        self.dogma_attributes = dogma_attributes
        self.dogma_effects = dogma_effects

    @staticmethod
    def from_dict(obj: Any) -> 'TypeDogma':
        assert isinstance(obj, dict)
        key = from_int(obj.get("_key"))
        dogma_attributes = from_list(DogmaAttribute.from_dict, obj.get("dogmaAttributes"))
        dogma_effects = from_union([lambda x: from_list(DogmaEffect.from_dict, x), from_none], obj.get("dogmaEffects"))
        return TypeDogma(key, dogma_attributes, dogma_effects)

    def to_dict(self) -> dict:
        result: dict = {}
        result["_key"] = from_int(self.key)
        result["dogmaAttributes"] = from_list(lambda x: to_class(DogmaAttribute, x), self.dogma_attributes)
        if self.dogma_effects is not None:
            result["dogmaEffects"] = from_union([lambda x: from_list(lambda x: to_class(DogmaEffect, x), x), from_none], self.dogma_effects)
        return result


def type_dogma_from_dict(s: Any) -> TypeDogma:
    return TypeDogma.from_dict(s)


def type_dogma_to_dict(x: TypeDogma) -> Any:
    return to_class(TypeDogma, x)
// To parse this data:
//
//   import { Convert, TypeDogma } from "./file";
//
//   const typeDogma = Convert.toTypeDogma(json);
//
// These functions will throw an error if the JSON doesn't
// match the expected interface, even if the JSON is valid.

export interface TypeDogma {
    _key:            number;
    dogmaAttributes: DogmaAttribute[];
    dogmaEffects?:   DogmaEffect[];
    [property: string]: any;
}

export interface DogmaAttribute {
    attributeID: number;
    value:       number;
    [property: string]: any;
}

export interface DogmaEffect {
    effectID:  number;
    isDefault: boolean;
    [property: string]: any;
}

// Converts JSON strings to/from your types
// and asserts the results of JSON.parse at runtime
export class Convert {
    public static toTypeDogma(json: string): TypeDogma {
        return cast(JSON.parse(json), r("TypeDogma"));
    }

    public static typeDogmaToJson(value: TypeDogma): string {
        return JSON.stringify(uncast(value, r("TypeDogma")), null, 2);
    }
}

function invalidValue(typ: any, val: any, key: any, parent: any = ''): never {
    const prettyTyp = prettyTypeName(typ);
    const parentText = parent ? ` on ${parent}` : '';
    const keyText = key ? ` for key "${key}"` : '';
    throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`);
}

function prettyTypeName(typ: any): string {
    if (Array.isArray(typ)) {
        if (typ.length === 2 && typ[0] === undefined) {
            return `an optional ${prettyTypeName(typ[1])}`;
        } else {
            return `one of [${typ.map(a => { return prettyTypeName(a); }).join(", ")}]`;
        }
    } else if (typeof typ === "object" && typ.literal !== undefined) {
        return typ.literal;
    } else {
        return typeof typ;
    }
}

function jsonToJSProps(typ: any): any {
    if (typ.jsonToJS === undefined) {
        const map: any = {};
        typ.props.forEach((p: any) => map[p.json] = { key: p.js, typ: p.typ });
        typ.jsonToJS = map;
    }
    return typ.jsonToJS;
}

function jsToJSONProps(typ: any): any {
    if (typ.jsToJSON === undefined) {
        const map: any = {};
        typ.props.forEach((p: any) => map[p.js] = { key: p.json, typ: p.typ });
        typ.jsToJSON = map;
    }
    return typ.jsToJSON;
}

function transform(val: any, typ: any, getProps: any, key: any = '', parent: any = ''): any {
    function transformPrimitive(typ: string, val: any): any {
        if (typeof typ === typeof val) return val;
        return invalidValue(typ, val, key, parent);
    }

    function transformUnion(typs: any[], val: any): any {
        // val must validate against one typ in typs
        const l = typs.length;
        for (let i = 0; i < l; i++) {
            const typ = typs[i];
            try {
                return transform(val, typ, getProps);
            } catch (_) {}
        }
        return invalidValue(typs, val, key, parent);
    }

    function transformEnum(cases: string[], val: any): any {
        if (cases.indexOf(val) !== -1) return val;
        return invalidValue(cases.map(a => { return l(a); }), val, key, parent);
    }

    function transformArray(typ: any, val: any): any {
        // val must be an array with no invalid elements
        if (!Array.isArray(val)) return invalidValue(l("array"), val, key, parent);
        return val.map(el => transform(el, typ, getProps));
    }

    function transformDate(val: any): any {
        if (val === null) {
            return null;
        }
        const d = new Date(val);
        if (isNaN(d.valueOf())) {
            return invalidValue(l("Date"), val, key, parent);
        }
        return d;
    }

    function transformObject(props: { [k: string]: any }, additional: any, val: any): any {
        if (val === null || typeof val !== "object" || Array.isArray(val)) {
            return invalidValue(l(ref || "object"), val, key, parent);
        }
        const result: any = {};
        Object.getOwnPropertyNames(props).forEach(key => {
            const prop = props[key];
            const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined;
            result[prop.key] = transform(v, prop.typ, getProps, key, ref);
        });
        Object.getOwnPropertyNames(val).forEach(key => {
            if (!Object.prototype.hasOwnProperty.call(props, key)) {
                result[key] = transform(val[key], additional, getProps, key, ref);
            }
        });
        return result;
    }

    if (typ === "any") return val;
    if (typ === null) {
        if (val === null) return val;
        return invalidValue(typ, val, key, parent);
    }
    if (typ === false) return invalidValue(typ, val, key, parent);
    let ref: any = undefined;
    while (typeof typ === "object" && typ.ref !== undefined) {
        ref = typ.ref;
        typ = typeMap[typ.ref];
    }
    if (Array.isArray(typ)) return transformEnum(typ, val);
    if (typeof typ === "object") {
        return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val)
            : typ.hasOwnProperty("arrayItems")    ? transformArray(typ.arrayItems, val)
            : typ.hasOwnProperty("props")         ? transformObject(getProps(typ), typ.additional, val)
            : invalidValue(typ, val, key, parent);
    }
    // Numbers can be parsed by Date but shouldn't be.
    if (typ === Date && typeof val !== "number") return transformDate(val);
    return transformPrimitive(typ, val);
}

function cast<T>(val: any, typ: any): T {
    return transform(val, typ, jsonToJSProps);
}

function uncast<T>(val: T, typ: any): any {
    return transform(val, typ, jsToJSONProps);
}

function l(typ: any) {
    return { literal: typ };
}

function a(typ: any) {
    return { arrayItems: typ };
}

function u(...typs: any[]) {
    return { unionMembers: typs };
}

function o(props: any[], additional: any) {
    return { props, additional };
}

function m(additional: any) {
    return { props: [], additional };
}

function r(name: string) {
    return { ref: name };
}

const typeMap: any = {
    "TypeDogma": o([
        { json: "_key", js: "_key", typ: 0 },
        { json: "dogmaAttributes", js: "dogmaAttributes", typ: a(r("DogmaAttribute")) },
        { json: "dogmaEffects", js: "dogmaEffects", typ: u(undefined, a(r("DogmaEffect"))) },
    ], "any"),
    "DogmaAttribute": o([
        { json: "attributeID", js: "attributeID", typ: 0 },
        { json: "value", js: "value", typ: 3.14 },
    ], "any"),
    "DogmaEffect": o([
        { json: "effectID", js: "effectID", typ: 0 },
        { json: "isDefault", js: "isDefault", typ: true },
    ], "any"),
};