Ejemplo n.º 1
0
 /// <summary>
 /// Encode the data value to json.
 /// </summary>
 /// <returns>The json value</returns>
 internal JsonValue fullEncode()
 {
     JsonObject data = new JsonObject()
         .add("type", this.getType())
         .add("value", this.encode());
     return data;
 }
Ejemplo n.º 2
0
 protected void fillData(JsonObject data)
 {
     if (data.get("snaks") != null)
     {
         foreach (JsonObject.Member member in data.get("snaks").asObject())
         {
             Dictionary<string, Snak> list = new Dictionary<string, Snak>();
             foreach (JsonValue value in member.value.asArray())
             {
                 Snak snak = Snak.newFromArray(value.asObject());
                 list.Add(snak.dataValue.getHash(), snak);
             }
             this.snaks.Add(member.name, list);
         }
     }
     if (data.get("hash") != null)
     {
         this.hash = data.get("hash").asString();
     }
     if (this.internalId == null)
     {
         if (this.hash != null)
         {
             this.internalId = this.hash;
         }
         else
         {
             this.internalId = "" + Environment.TickCount + this.statement.internalId;
         }
     }
 }
Ejemplo n.º 3
0
        /* WS */
        public ScarletMetrics()
        {
            metricsws = new WebSocket("ws://" + MetricsWSURL + ":" + MetricsWSPort);

            metricsws.Connect();

            metricsws.OnOpen += (sender, e) =>
            {
                // Metrics - On Connection to the Metrics WS Reporting Server
                // Need to include username / IP in here as well.
                JsonObject jsonMessage = new JsonObject()
                    .add("type", "metrics")
                    .add("message", "connected");
                metricsws.Send(jsonMessage.ToString());
            };

            metricsws.OnMessage += (sender, e) =>
            {
            };

            metricsws.OnClose += (sender, e ) =>
                metricsws.Connect();

            metricsws.OnError += (sender, e) =>
                metricsws.Connect();
        }
Ejemplo n.º 4
0
 protected override void fillData(JsonObject data)
 {
     base.fillData(data);
     if (data.get("datatype") != null)
     {
         this.datatype = data.get("datatype").asString();
     }
 }
Ejemplo n.º 5
0
 internal JsonObject toArray()
 {
     JsonObject data = new JsonObject()
         .add("snaktype", this.type)
         .add("property", this.propertyId.getPrefixedId());
     if (this.dataValue != null)
     {
         data.add("datavalue", this.dataValue.fullEncode());
     }
     return data;
 }
Ejemplo n.º 6
0
 protected override void fillData(JsonObject data)
 {
     base.fillData(data);
     if ( data.get("sitelinks") != null )
     {
         this.sitelinks.Clear();
         foreach ( JsonObject.Member member in data.get("sitelinks").asObject() )
         {
             JsonObject obj = member.value.asObject();
             this.sitelinks.Add(obj.get("site").asString(), obj.get("title").asString());
         }
     }        }
Ejemplo n.º 7
0
 internal static Claim newFromArray(Entity entity, JsonObject data)
 {
     if (data.get("type") != null)
     {
         switch (data.get("type").asString())
         {
             case "statement":
                 return new Statement(entity, data);
             default:
                 return new Claim(entity, data);
         }
     }
     throw new Exception("Unknown type");
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Create a list of entities form an api response.
 /// </summary>
 /// <param name="result">The result of the api request</param>
 /// <returns>The list of entities</returns>
 protected Entity[] parseGetEntitiesApiResponse(JsonObject result)
 {
     List<Entity> entities = new List<Entity>();
     if (result.get("entities") != null)
     {
         foreach (JsonObject.Member member in result.get("entities").asObject())
         {
             if (member.value.asObject().get("missing") == null)
             {
                 entities.Add(Entity.newFromArray(this, member.value.asObject()));
             }
         }
     }
     return entities.ToArray();
 }
Ejemplo n.º 9
0
 private JsonObject(JsonObject obj, bool unmodifiable)
 {
     if (obj == null)
     {
         throw new ArgumentNullException("obj");
     }
     if (unmodifiable)
     {
         values = new ReadOnlyDictionary<string, JsonValue>(obj.values);
     }
     else
     {
         values = new Dictionary<string, JsonValue>(obj.values);
     }
 }
Ejemplo n.º 10
0
 protected override void fillData(JsonObject data)
 {
     base.fillData(data);
     if (data.get("rank") != null)
     {
         this.rank = data.get("rank").asString();
     }
     if (data.get("references") != null)
     {
         foreach (JsonValue value in data.get("references").asArray())
         {
             Reference reference = new Reference(this, value.asObject());
             this.references.Add(reference.internalId, reference);
         }
     }
 }
Ejemplo n.º 11
0
 protected virtual void fillData(JsonObject data)
 {
     if (data.get("mainsnak") != null)
     {
         this.mMainSnak = Snak.newFromArray(data.get("mainsnak").asObject());
     }
     if (data.get("id") != null)
     {
         this.id = data.get("id").asString();
     }
     if (this.internalId == null)
     {
         if (this.id != null)
         {
             this.internalId = this.id;
         }
         else
         {
             this.internalId = "" + Environment.TickCount + this.mMainSnak.propertyId + this.mMainSnak.dataValue;
         }
     }
 }
Ejemplo n.º 12
0
 internal static Snak newFromArray(JsonObject data)
 {
     if (data.get("snaktype") == null || data.get("property") == null)
     {
         throw new ArgumentException("Invalid Snak serialization", "data");
     }
     var dataValue = data.get("datavalue");
     if ( dataValue != null )
     {
         return new Snak(
            data.get("snaktype").asString(),
            EntityId.newFromPrefixedId(data.get("property").asString()),
            DataValueFactory.newFromArray(dataValue.asObject())
            );
     }
     else
     {
         return new Snak(
            data.get("snaktype").asString(),
            EntityId.newFromPrefixedId(data.get("property").asString()),
            null
            );
     }
 }
Ejemplo n.º 13
0
        private IEnumerable<Entity> ParseJson(JsonObject data)
        {
            var result = new List<Entity>();
            var actualData = data.get("aaData");
            if ( actualData != null )
            {
                var array = actualData.asArray();
                foreach ( JsonArray item in array )
                {
                    var parsedData = new List<String>();
                    foreach ( JsonValue dataPoint in item )
                    {
                        var strippedText = Regex.Replace(dataPoint.asString(), "<.*?>", string.Empty).Replace(",", String.Empty);
                        if ( strippedText == "-" )
                        {
                            strippedText = "0";
                        }
                        parsedData.Add(strippedText);
                    }
                    var firstLine = parsedData.First();
                    if ( !String.IsNullOrWhiteSpace(firstLine) && (firstLine != "00") )
                    {
                        Entity entity = new Entity();
                        entity.ParseName(parsedData.ElementAt(1).Replace("ท้องถิ่น", String.Empty).Trim());
                        entity.geocode = Convert.ToUInt32(firstLine, CultureInfo.InvariantCulture);
                        while ( entity.geocode % 100 == 0 )
                        {
                            entity.geocode = entity.geocode / 100;
                        }

                        PopulationData population = CreateEmptyPopulationEntry();
                        entity.population.Add(population);
                        HouseholdDataPoint householdDataPoint = new HouseholdDataPoint();
                        householdDataPoint.male = Convert.ToInt32(parsedData.ElementAt(2), CultureInfo.InvariantCulture);
                        householdDataPoint.female = Convert.ToInt32(parsedData.ElementAt(3), CultureInfo.InvariantCulture);
                        householdDataPoint.total = Convert.ToInt32(parsedData.ElementAt(4), CultureInfo.InvariantCulture);
                        householdDataPoint.households = Convert.ToInt32(parsedData.ElementAt(5), CultureInfo.InvariantCulture);
                        population.data.Add(householdDataPoint);
                        if ( (householdDataPoint.total > 0) || (householdDataPoint.households > 0) )
                        {
                            // occasionally there are empty entries, e.g. for 3117 includes an empty 311102
                            result.Add(entity);
                        }
                    }
                }
            }
            return result;
        }
Ejemplo n.º 14
0
 internal Property(WikibaseApi api, JsonObject data) : base(api, data) { }
Ejemplo n.º 15
0
 public void Send(string message)
 {
     JsonObject jsonMessage = new JsonObject();
     jsonMessage.add("type", "metrics").add("message", message);
     metricsws.Send(jsonMessage.ToString());
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Save the reference.
 /// </summary>
 /// <param name="summary">The summary</param>
 public void save(string summary)
 {
     if (this.statement.id == null)
     {
         throw new Exception("The statement has no Id. Please save the statement containing it first.");
     }
     JsonObject obj = new JsonObject();
     foreach (KeyValuePair<string, Dictionary<string, Snak>> pair in this.snaks)
     {
         JsonArray array = new JsonArray();
         foreach(KeyValuePair<string, Snak> p in pair.Value)
         {
             array.add(p.Value.toArray());
         }
         obj.add(pair.Key, array);
     }
     JsonObject result = this.statement.entity.api.setReference(this.statement.id, obj, this.hash, this.statement.entity.lastRevisionId, summary);
     this.updateDataFromResult(result);
 }
Ejemplo n.º 17
0
 internal void writeObject(JsonObject obj)
 {
     writeBeginObject();
     bool first = true;
     foreach (JsonObject.Member member in obj)
     {
         if (!first)
         {
             writeObjectValueSeparator();
         }
         writeString(member.name);
         writeNameValueSeparator();
         member.value.write(this);
         first = false;
     }
     writeEndObject();
 }
Ejemplo n.º 18
0
 internal Reference(Statement statement, JsonObject data)
 {
     this.statement = statement;
     this.fillData(data);
 }
Ejemplo n.º 19
0
 internal Statement(Entity entity, JsonObject data) : base(entity, data) { }
Ejemplo n.º 20
0
 /// <summary>
 /// Create an entity.
 /// </summary>
 /// <param name="type">The type of the entity</param>
 /// <param name="data">The serialized data of the entity</param>
 /// <param name="baseRevisionId">The numeric identifier for the revision to base the modification on</param>
 /// <param name="summary">The summary for the change</param>
 /// <returns>The result</returns>
 internal JsonObject createEntity(string type, JsonObject data, int baseRevisionId, string summary)
 {
     Dictionary<string, string> parameters = new Dictionary<string, string>()
     {
         { "action", "wbeditentity" }
     };
     Dictionary<string, string> postFields = new Dictionary<string, string>()
     {
         { "data", data.ToString() },
         { "new", type }
     };
     return this.editAction(parameters, postFields, baseRevisionId, summary);
 }
Ejemplo n.º 21
0
 internal Item(WikibaseApi api, JsonObject data) : base(api, data) { }
Ejemplo n.º 22
0
 internal void updateLastRevisionIdFromResult(JsonObject result)
 {
     if (result.get("pageinfo") != null && result.get("pageinfo").asObject().get("lastrevid") != null)
     {
         this.lastRevisionId = result.get("pageinfo").asObject().get("lastrevid").asInt();
     }
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Save all changes.
 /// </summary>
 /// <param name="summary">The edit summary</param>
 public void save(string summary)
 {
     if (!this.changes.isEmpty())
     {
         JsonObject result;
         if(this.id == null)
         {
             result = this.api.createEntity(this.getType(), this.changes, this.lastRevisionId, summary);
         }
         else
         {
             result = this.api.editEntity(this.id.getPrefixedId(), this.changes, this.lastRevisionId, summary);
         }
         if (result.get("entity") != null)
         {
             this.fillData(result.get("entity").asObject());
         }
         this.updateLastRevisionIdFromResult(result);
         this.changes = new JsonObject();
     }
 }
Ejemplo n.º 24
0
 /// <summary>
 /// Set a reference.
 /// </summary>
 /// <param name="statement">GUID identifying the statement</param>
 /// <param name="snaks">The snaks to set the reference to. Array with property ids pointing to arrays containing the snaks for that property</param>
 /// <param name="reference">A hash of the reference that should be updated. When not provided, a new reference is created</param>
 /// <param name="baseRevisionId">The numeric identifier for the revision to base the modification on</param>
 /// <param name="summary">The summary for the change</param>
 /// <returns>The result</returns>
 internal JsonObject setReference(string statement, JsonObject snaks, string reference, int baseRevisionId, string summary)
 {
     Dictionary<string, string> parameters = new Dictionary<string, string>()
     {
         { "action", "wbsetreference" },
         { "statement", statement },
         { "snaks", snaks.ToString() }
     };
     if (reference != null)
     {
         parameters["reference"] = reference;
     }
     return this.editAction(parameters, new Dictionary<string, string>(), baseRevisionId, summary);
 }
Ejemplo n.º 25
0
 internal static Entity newFromArray(WikibaseApi api, JsonObject data)
 {
     if (data.get("type") != null)
     {
         switch (data.get("type").asString())
         {
             case "item":
                 return new Item(api, data);
             case "property":
                 return new Property(api, data);
         }
     }
     throw new Exception("Unknown type");
 }
Ejemplo n.º 26
0
 internal static DataValue newFromArray(JsonObject data)
 {
     return newDataValue(data.get("type").asString(), data.get("value"));
 }
Ejemplo n.º 27
0
 internal Entity(WikibaseApi api, JsonObject data)
 {
     this.api = api;
     this.fillData(data);
 }
Ejemplo n.º 28
0
 protected void updateDataFromResult(JsonObject result)
 {
     if (result.get("claim") != null)
     {
         this.fillData(result.get("claim").asObject());
     }
     this.entity.updateLastRevisionIdFromResult(result);
 }
Ejemplo n.º 29
0
 protected virtual void fillData(JsonObject data)
 {
     if (data.get("id") != null)
     {
         this.id = EntityId.newFromPrefixedId(data.get("id").asString());
     }
     if (data.get("lastrevid") != null)
     {
         this.lastRevisionId = data.get("lastrevid").asInt();
     }
     JsonValue returnedLabels = data.get("labels");
     if ( (returnedLabels != null) && (returnedLabels.isObject()) )
     if (data.get("labels") != null)
     {
             labels.Clear();
             foreach ( JsonObject.Member member in returnedLabels.asObject() )
         {
             JsonObject obj = member.value.asObject();
             this.labels.Add(obj.get("language").asString(), obj.get("value").asString());
         }
     }
     JsonValue returnedDescriptions = data.get("descriptions");
     if ( (returnedDescriptions != null) && (returnedDescriptions.isObject()) )
     {
         descriptions.Clear();
         foreach ( JsonObject.Member member in returnedDescriptions.asObject() )
         {
             JsonObject obj = member.value.asObject();
             this.descriptions.Add(obj.get("language").asString(), obj.get("value").asString());
         }
     }
     JsonValue returnedAliases = data.get("aliases");
     if ( (returnedAliases != null) && (returnedAliases.isObject()) )
     {
         // strange - after save an empty array is returned, whereas by a normal get the fully alias list is returned
         aliases.Clear();
         foreach ( JsonObject.Member member in returnedAliases.asObject() )
         {
             List<String> list = new List<String>();
             foreach (JsonValue value in member.value.asArray())
             {
                 list.Add(value.asObject().get("value").asString());
             }
             this.aliases.Add(member.name, list);
         }
     }
     JsonValue returnedClaims = data.get("claims");
     if ( (returnedClaims != null) && (returnedClaims.isObject()) )
     {
         claims.Clear();
         foreach ( JsonObject.Member member in returnedClaims.asObject() )
         {
             Dictionary<String, Claim> list = new Dictionary<String, Claim>();
             foreach (JsonValue value in member.value.asArray())
             {
                 Claim claim = Claim.newFromArray(this, value.asObject());
                 list.Add(claim.internalId, claim);
             }
             this.claims.Add(member.name, list);
         }
     }
 }
Ejemplo n.º 30
0
 internal Claim(Entity entity, JsonObject data)
 {
     this.entity = entity;
     this.fillData(data);
 }