示例#1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject   item          = JObject.Load(reader);
            string    discriminator = (string)item["type"];
            Datavalue datavalue;

            switch (discriminator)
            {
            case "string":
                datavalue = new DatavalueString();
                break;

            case "wikibase-entityid":
                datavalue = new DatavalueItem();
                break;

            case "globecoordinate":
                datavalue = new DatavalueCoordinate();
                break;

            case "time":
                datavalue = new DatavalueTime();
                break;

            case "quantity":
                datavalue = new DatavalueQuantity();
                break;

            case "monolingualtext":
                datavalue = new DatavalueQuantity();
                break;

            default:
                datavalue = null;
                break;
            }
            serializer.Populate(item.CreateReader(), datavalue);
            return(datavalue);
        }
示例#2
0
        public static Datavalue CreateDataValue(string value, typeData type)
        {
            string[] val = value.Split('|');
            switch (type)
            {
            case typeData.String:     //0=string
                DatavalueString tmpS = new DatavalueString();
                tmpS.type  = "string";
                tmpS.value = val[0];
                return(tmpS);

            case typeData.Monolingual:     //0=language, 1=text
                DatavalueMonolingual tmpM = new DatavalueMonolingual();
                tmpM.type           = "monolingualtext";
                tmpM.value.language = val[0];
                tmpM.value.text     = val[1];
                return(tmpM);

            case typeData.Item:     //0=item with or without "Q"
                DatavalueItem tmpW = new DatavalueItem();
                tmpW.type              = "wikibase-entityid";
                tmpW.value.numeric_id  = Convert.ToInt32(val[0].Replace("Q", "").Replace("q", ""));
                tmpW.value.entity_type = "item";
                return(tmpW);

            case typeData.Coordinate:     //0=latitude, 1=longitude, 2=precision 3=globe (if not declared use Q2)
                DatavalueCoordinate tmpC = new DatavalueCoordinate();
                tmpC.type            = "globecoordinate";
                tmpC.value.latitude  = val[0];  // decimal: no default, 9 digits after the dot and two before, signed
                tmpC.value.longitude = val[1];  // decimal: no default, 9 digits after the dot and three before, signed
                tmpC.value.precision = val[2];  // decimal, representing degrees of distance, defaults to 0, 9 digits after the dot and three before, unsigned, used to save the precision of the representation
                tmpC.value.altitude  = null;    //unmanaged
                if (val.Count() < 4)
                {
                    tmpC.value.globe = "http://www.wikidata.org/entity/Q2";
                }
                else
                {
                    tmpC.value.globe = val[3];
                }
                return(tmpC);

            case typeData.Time:     //0=time, 1=timezone, 2=before, 3=after, 4=precision, 5=calendarmodel (if not declared use http://www.wikidata.org/entity/Q1985727)
                DatavalueTime tmpT = new DatavalueTime();
                tmpT.type            = "time";
                tmpT.value.time      = val[0]; // string isotime: point in time, represented per ISO8601, they year always having 11 digits, the date always be signed, in the format +00000002013-01-01T00:00:00Z
                tmpT.value.timezone  = val[1]; // signed integer: Timezone information as an offset from UTC in minutes
                tmpT.value.before    = val[2]; // integer: If the date is uncertain, how many units before the given time could it be? the unit is given by the precision
                tmpT.value.after     = val[3]; // integer: If the date is uncertain, how many units after the given time could it be? the unit is given by the precision
                tmpT.value.precision = val[4]; // shortint: 0 - billion years, 1 - hundred million years, ..., 6 - millenia, 7 - century, 8 - decade, 9 - year, 10 - month, 11 - day, 12 - hour, 13 - minute, 14 - second
                if (val.Count() < 6)
                {
                    tmpT.value.calendarmodel = "http://www.wikidata.org/entity/Q1985727";     // URI identifying the calendar model that should be used to display this time value. Note that time is always saved in proleptic Gregorian, this URI states how the value should be displayed
                }
                else
                {
                    tmpT.value.calendarmodel = val[5];
                }

                return(tmpT);

            case typeData.Quantity:     //0=item without
                DatavalueQuantity tmpQ = new DatavalueQuantity();
                tmpQ.type             = "quantity";
                tmpQ.value.amount     = val[0];
                tmpQ.value.unit       = val[1];
                tmpQ.value.upperBound = val[2];
                tmpQ.value.lowerBound = val[3];
                return(tmpQ);

            default:
                return(null);
            }
        }