Exemplo n.º 1
0
        private IVariable ParseDateVariable(IO.JsonReader jr)
        {
            // init
            IVariable result = null;

            // node type
            switch (jr.NodeType)
            {
            case IO.JsonNodeType.String:
                result = new TextVariable((string)jr.Value);
                jr.Read();
                break;

            case IO.JsonNodeType.StartOfObject:
                // init
                int?[][]     dateParts = new int?[][] { };
                bool?        circa     = null;
                DateVariable?raw       = null;
                string       literal   = null;
                Season?      season    = null;

                // skip
                jr.Read();

                // read properties
                while (jr.NodeType == IO.JsonNodeType.PropertyName)
                {
                    // init
                    var propertyName = (string)jr.Value;
                    jr.Read();

                    // property
                    switch (propertyName.ToLower())
                    {
                    case "date-parts":
                        dateParts = this.ParseDateVariableDateParts(jr);
                        break;

                    case "raw":
                        raw = ParseRawDate(jr);
                        break;

                    case "literal":
                        literal = jr.ReadAsString();
                        break;

                    case "circa":
                        circa = jr.ReadAsBoolean();
                        break;

                    case "season":
                        season = this.ParseSeason(jr);
                        break;

                    default:
                        throw new DataFormatException(jr, "Unexpected property '{0}'.", propertyName);
                    }
                }

                // valid date?
                if (raw.HasValue)
                {
                    // raw
                    if (dateParts.Length > 0 || literal != null)
                    {
                        throw new DataFormatException(jr, "Date parts and literal properties are not allowed for raw date variables.");
                    }

                    // done
                    result = new DateVariable(raw.Value.YearFrom, raw.Value.SeasonFrom, raw.Value.MonthFrom, raw.Value.DayFrom, raw.Value.YearTo, raw.Value.SeasonTo, raw.Value.MonthTo, raw.Value.DayTo, circa ?? false);
                }
                else if (literal != null)
                {
                    // literal
                    if (dateParts.Length > 0 || raw != null || circa != null)
                    {
                        throw new DataFormatException(jr, "Date parts, raw and circa properties are not allowed for literal date variables.");
                    }

                    // done
                    result = new TextVariable(literal);
                }
                else
                {
                    // dateparts
                    switch (dateParts.Length)
                    {
                    case 0:
                        result = null;
                        break;

                    case 1:
                        result = new DateVariable(dateParts[0][0].Value, season, dateParts[0][1], dateParts[0][2], circa ?? false);
                        break;

                    case 2:
                        result = new DateVariable(dateParts[0][0].Value, season, dateParts[0][1], dateParts[0][2], dateParts[1][0].Value, season, dateParts[1][1], dateParts[1][2], circa ?? false);
                        break;

                    default:
                        throw new DataFormatException(jr, "Invalid date variable.");
                    }
                }

                // end object
                jr.Read(IO.JsonNodeType.EndOfObject);

                break;

            default:
                throw new DataFormatException(jr, "Json token '{0}' cannot be parsed into a text variable.", jr.NodeType);
            }

            // done
            return(result);
        }
Exemplo n.º 2
0
        private INamesVariable ParseNamesVariable(IO.JsonReader jr)
        {
            // init
            var results = new List <Name>();

            // start of array
            jr.Read(IO.JsonNodeType.StartOfArray);

            // objects are names
            while (jr.NodeType == IO.JsonNodeType.StartOfObject)
            {
                // skip
                jr.Read();

                // init
                string family               = null;
                string given                = null;
                string suffix               = null;
                bool?  commaSuffix          = null;
                string droppingParticles    = null;
                string nonDroppingParticles = null;
                bool?  isInstitution        = null;
                string literal              = null;

                // properties
                while (jr.NodeType == IO.JsonNodeType.PropertyName)
                {
                    // init
                    var propertyName = (string)jr.Value;

                    // skip
                    jr.Read();

                    // read value
                    switch (propertyName.ToLower())
                    {
                    case "family":
                        family = jr.ReadAsString();
                        break;

                    case "given":
                        given = jr.ReadAsString();
                        break;

                    case "suffix":
                        suffix = jr.ReadAsString();
                        break;

                    case "dropping-particle":
                        droppingParticles = jr.ReadAsString();
                        break;

                    case "non-dropping-particle":
                        nonDroppingParticles = jr.ReadAsString();
                        break;

                    case "comma-suffix":
                        commaSuffix = jr.ReadAsBoolean();
                        break;

                    case "isinstitution":
                        isInstitution = jr.ReadAsBoolean();
                        break;

                    case "literal":
                        literal = jr.ReadAsString();
                        break;

                    default:
                        throw new DataFormatException(jr, "Unexpected property '{0}'.", propertyName);
                    }
                }

                // done
                if (isInstitution ?? false)
                {
                    // institution
                    if (family == null)
                    {
                        throw new DataFormatException(jr, "The family property is required for institutional names.");
                    }
                    else if (!string.IsNullOrEmpty(given) || !string.IsNullOrEmpty(suffix) || commaSuffix.HasValue || !string.IsNullOrEmpty(droppingParticles) || !string.IsNullOrEmpty(nonDroppingParticles))
                    {
                        throw new DataFormatException(jr, "Only the family property is allowed for institutional names.");
                    }

                    // done
                    results.Add(new InstitutionalName(family));
                }
                else if (literal != null)
                {
                    // institiution
                    if (!string.IsNullOrEmpty(family) || !string.IsNullOrEmpty(given) || !string.IsNullOrEmpty(suffix) || commaSuffix.HasValue || !string.IsNullOrEmpty(droppingParticles) || !string.IsNullOrEmpty(nonDroppingParticles))
                    {
                        throw new DataFormatException(jr, "No other proeprties are allowed for literal names.");
                    }

                    // done
                    results.Add(new InstitutionalName(literal));
                }
                else
                {
                    // personal
                    results.Add(new PersonalName(family, given, suffix, commaSuffix ?? false, droppingParticles, nonDroppingParticles));
                }

                // read end of object
                jr.Read(IO.JsonNodeType.EndOfObject);
            }

            // end of array
            jr.Read(IO.JsonNodeType.EndOfArray);

            // done
            return(new NamesVariable(results));
        }