/// <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; }
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; } } }
/* 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(); }
protected override void fillData(JsonObject data) { base.fillData(data); if (data.get("datatype") != null) { this.datatype = data.get("datatype").asString(); } }
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; }
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()); } } }
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"); }
/// <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(); }
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); } }
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); } } }
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; } } }
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 ); } }
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; }
internal Property(WikibaseApi api, JsonObject data) : base(api, data) { }
public void Send(string message) { JsonObject jsonMessage = new JsonObject(); jsonMessage.add("type", "metrics").add("message", message); metricsws.Send(jsonMessage.ToString()); }
/// <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); }
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(); }
internal Reference(Statement statement, JsonObject data) { this.statement = statement; this.fillData(data); }
internal Statement(Entity entity, JsonObject data) : base(entity, data) { }
/// <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); }
internal Item(WikibaseApi api, JsonObject data) : base(api, data) { }
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(); } }
/// <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(); } }
/// <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); }
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"); }
internal static DataValue newFromArray(JsonObject data) { return newDataValue(data.get("type").asString(), data.get("value")); }
internal Entity(WikibaseApi api, JsonObject data) { this.api = api; this.fillData(data); }
protected void updateDataFromResult(JsonObject result) { if (result.get("claim") != null) { this.fillData(result.get("claim").asObject()); } this.entity.updateLastRevisionIdFromResult(result); }
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); } } }
internal Claim(Entity entity, JsonObject data) { this.entity = entity; this.fillData(data); }