/// <summary> /// Adds or updates the passed in BSON document /// </summary> public bool AddOrUpdateRecord(JsonRecord record, string collectionName) { return(AddOrUpdate( record.Id, record.Data, collectionName)); }
// expecting something like "key":{"item":"content"[, ...]} // or {"item":"content"[, ...]} // straight forward and not really nice - should do it.. public static JsonRecord Decompose(string js) { var record = new JsonRecord( ); // do some houskeeping first if (string.IsNullOrWhiteSpace(js)) { return(null); // no usable content } js = js.Replace("\n", "").Replace("\r", "").Trim( ); // cleanup any CR, LFs and whitespaces if (js.EndsWith(",")) { js = js.Substring(0, js.Length - 1).TrimEnd( ); // cut end comma and clean } // divide key and content - if needed string key = ""; int colPos = js.IndexOf('{'); if (colPos > 1) { // would be key.. colPos = js.IndexOf(':'); if (colPos < 1) { return(null); // no key element at all } key = RemoveApo(js.Substring(0, colPos)); js = js.Substring(colPos + 1).TrimStart( ); // cut the key part and clean } // starting brace must be at the beginning now if (!js.StartsWith("{")) { return(null); // no content at all ?? key only is disregarded here } if (!js.EndsWith("}")) { return(null); // misalinged content not { something } } js = js.Substring(1, js.Length - 2).Trim( ); // we should be left with 'content, content' here IList <string> contList = Split(js, ','); foreach (var item in contList) { IList <string> itemPairs = Split(item, ':'); if (!record.ContainsKey(key)) { record.Add(key, new JsonContent( )); } record[key].Add(RemoveApo(itemPairs[0]), RemoveApo(itemPairs[1])); } return(record); }
public IActionResult Post([FromBody] JsonRecord input) { try { var parsed = RecordParser.TryParseLine(input.RecordInput).Item1; AddRecord(parsed); return(Ok()); } catch { return(BadRequest()); } }
public void T_Parser1() { string sIn = "\"01001\": {\"r\": \"V5-NAM\",\"t\": \"F900\"},"; JsonRecord retVal = JsonParser.Decompose(sIn); // removes decoration Assert.AreEqual <int>(1, retVal.Count); Assert.AreEqual <bool>(true, retVal.ContainsKey("01001")); Assert.AreEqual <bool>(true, retVal["01001"].ContainsKey("r")); Assert.AreEqual <bool>(true, retVal["01001"].ContainsKey("t")); Assert.AreEqual <string>("V5-NAM", retVal["01001"]["r"]); Assert.AreEqual <string>("F900", retVal["01001"]["t"]); }
/// <summary> /// Returns a new Icao Record from given Jason /// </summary> /// <param name="js">The record as Jason fragment</param> private static icaoRec FromNative(string js) { JsonRecord jRec = JsonParser.Decompose(js); if (jRec?.Count > 0) { var reg = !jRec.Values[0].ContainsKey("r") ? "" : jRec.Values[0]["r"]; var typ = !jRec.Values[0].ContainsKey("t") ? "" : jRec.Values[0]["t"]; var iRec = new icaoRec(jRec.Keys[0].ToUpperInvariant(), reg, typ); return(iRec); } else { return(new icaoRec("", "", "")); } }
/// <summary> /// Adds or updates a document in the collection /// </summary> /// <param name="id">The id of the document</param> /// <param name="item"> /// The object that is being stored in the collection /// </param> public void AddOrUpdate(Guid id, T item) { if (this.Format == StorageFormat.BSON) { var record = new BsonRecord { Id = id, Data = Serializer.SerializeBson <T>(item) }; provider.AddOrUpdateRecord(record, this.Name); } else { var record = new JsonRecord { Id = id, Data = Serializer.SerializeJson <T>(item) }; provider.AddOrUpdateRecord(record, this.Name); } }
/// <summary> /// Returns a new Icao Record from given Jason /// </summary> /// <param name="native">The record as Jason fragment</param> private static icaoActRec FromNative(string native) { JsonRecord jRec = JsonParser.Decompose(native); if (jRec?.Count > 0) { var icao = !jRec.Values[0].ContainsKey("Designator") ? "" : jRec.Values[0]["Designator"].ToUpperInvariant( ); var desc = !jRec.Values[0].ContainsKey("Description") ? "" : jRec.Values[0]["Description"].ToUpperInvariant( ); var wtc = !jRec.Values[0].ContainsKey("WTC") ? "" : jRec.Values[0]["WTC"].ToUpperInvariant( ); var mfn = !jRec.Values[0].ContainsKey("ModelFullName") ? "" : jRec.Values[0]["ModelFullName"]; var mcod = !jRec.Values[0].ContainsKey("ManufacturerCode") ? "" : jRec.Values[0]["ManufacturerCode"].ToUpperInvariant( ); var acd = !jRec.Values[0].ContainsKey("AircraftDescription") ? "" : jRec.Values[0]["AircraftDescription"]; var ec = !jRec.Values[0].ContainsKey("EngineCount") ? "" : jRec.Values[0]["EngineCount"]; var et = !jRec.Values[0].ContainsKey("EngineType") ? "" : jRec.Values[0]["EngineType"]; var iRec = new icaoActRec(icao.ToUpperInvariant( ), desc, wtc, mfn, mcod, acd, ec, et); return(iRec); } else { return(null); } }
public bool AddOrUpdateRecord(JsonRecord record, string collectionName) { return(this.AddOrUpdateRecord( (IRecord <string>)record, collectionName)); }