static JsonValue ReadArray(string json, ref int i) { i++; // Skip the '[' SkipWhitespace(json, ref i); JsonValue arrayval = new JsonValue(); arrayval.Type = JsonType.Array; bool expectingValue = false; while (json[i] != ']') { expectingValue = false; arrayval.Add(ReadValue(json, ref i)); SkipWhitespace(json, ref i); if (json[i] == ',') { expectingValue = true; i++; SkipWhitespace(json, ref i); } else if (json[i] != ']') { throw new InvalidJsonException("Expected end array token at column " + i + "!"); } } if (expectingValue) { throw new InvalidJsonException("Unexpected end array token at column " + i + "!"); } i++; // Skip the ']' return arrayval; }
/// <summary> /// <para>Allows you to edit Linked Items for TechTypes.</para> /// <para>Can be used for existing TechTypes too.</para> /// </summary> /// <param name="techType">The TechType whose TechData you want to edit.</param> /// <param name="linkedItems">The collection of Ingredients for that TechType.</param> /// <seealso cref="Ingredient"/> void ICraftDataHandler.SetLinkedItems(TechType techType, ICollection <TechType> linkedItems) { if (!CraftDataPatcher.CustomTechData.TryGetValue(techType, out JsonValue smlJsonValue)) { CraftDataPatcher.CustomTechData.Add(techType, new JsonValue()); smlJsonValue = CraftDataPatcher.CustomTechData[techType]; } if (!smlJsonValue.Contains(TechData.PropertyToID("linkedItems"))) { smlJsonValue.Add(TechData.PropertyToID("linkedItems"), new JsonValue(JsonValue.Type.Array)); } else { smlJsonValue[TechData.PropertyToID("linkedItems")] = new JsonValue(JsonValue.Type.Array); } JsonValue linkedItemslist = smlJsonValue[TechData.PropertyToID("linkedItems")]; int current = 0; foreach (TechType i in linkedItems) { linkedItemslist.Add(new JsonValue(current)); linkedItemslist[current] = new JsonValue((int)i); current++; } }
/// <summary> /// <para>Allows you to edit recipes for TechTypes.</para> /// <para>Can be used for existing TechTypes too.</para> /// </summary> /// <param name="techType">The TechType whose TechData you want to edit.</param> /// <param name="ingredients">The collection of Ingredients for that TechType.</param> /// <seealso cref="Ingredient"/> void ICraftDataHandler.SetIngredients(TechType techType, ICollection <Ingredient> ingredients) { if (!CraftDataPatcher.CustomTechData.TryGetValue(techType, out JsonValue smlJsonValue)) { smlJsonValue = new JsonValue(); CraftDataPatcher.CustomTechData.Add(techType, smlJsonValue); } if (!smlJsonValue.Contains(TechData.PropertyToID("ingredients"))) { smlJsonValue.Add(TechData.PropertyToID("ingredients"), new JsonValue(JsonValue.Type.Array)); } else { smlJsonValue[TechData.PropertyToID("ingredients")] = new JsonValue(JsonValue.Type.Array); } JsonValue ingredientslist = smlJsonValue[TechData.PropertyToID("ingredients")]; int amount = TechData.PropertyToID("amount"); int tech = TechData.PropertyToID("techType"); int current = 0; foreach (Ingredient i in ingredients) { ingredientslist.Add(new JsonValue(current)); ingredientslist[current] = new JsonValue(JsonValue.Type.Object) { { amount, new JsonValue(i.amount) }, { tech, new JsonValue((int)i.techType) } }; current++; } }
/// <summary> /// <para>Allows you to add or edit TechData for TechTypes.</para> /// <para>Can be used for existing TechTypes too.</para> /// </summary> /// <param name="techType">The TechType whose TechData you want to edit.</param> /// <param name="jsonValue">The TechData for that TechType.</param> /// <seealso cref="TechData.defaults"/> void ICraftDataHandler.SetTechData(TechType techType, JsonValue jsonValue) { var techTypeValue = new JsonValue((int)techType); if (techTypeValue != jsonValue[TechData.PropertyToID("techType")]) { jsonValue.Add(TechData.PropertyToID("techType"), techTypeValue); } CraftDataPatcher.CustomTechData[techType] = jsonValue; }
public static JsonValue CreateJsonByStrList(List <string> intList) { if (intList == null) { return(null); } JsonValue json = JsonValue.Array(); for (int index = 0; index < intList.Count; index++) { json.Add(intList[index]); } return(json); }
static void setDouble(this JsonValue jv, int id, double val) { if (!jv.CheckType(JsonValue.Type.Object)) { return; } if (!jv.dataObject.TryGetValue(id, out JsonValue doubleValue)) { doubleValue = new JsonValue(JsonValue.Type.Double); jv.Add(id, doubleValue); } doubleValue.SetDouble(val); }
/// <summary> /// <para>Allows you to edit Linked Items for TechTypes.</para> /// <para>Can be used for existing TechTypes too.</para> /// </summary> /// <param name="techType">The TechType whose TechData you want to edit.</param> /// <param name="linkedItems">The collection of Ingredients for that TechType.</param> /// <seealso cref="Ingredient"/> void ICraftDataHandler.AddLinkedItems(TechType techType, ICollection <TechType> linkedItems) { if (!CraftDataPatcher.CustomTechData.TryGetValue(techType, out JsonValue smlJsonValue)) { smlJsonValue = new JsonValue(); CraftDataPatcher.CustomTechData.Add(techType, smlJsonValue); } smlJsonValue.Add(TechData.PropertyToID("linkedItems"), new JsonValue(JsonValue.Type.Array)); JsonValue linkedItemslist = smlJsonValue[TechData.PropertyToID("linkedItems")]; //int amount = TechData.PropertyToID("amount"); //int tech = TechData.PropertyToID("techType"); //int count = linkedItems.Count; int current = 0; foreach (TechType i in linkedItems) { linkedItemslist.Add(new JsonValue(current)); linkedItemslist[current] = new JsonValue((int)i); current++; } }
public void ArrayOperations() { JsonValue test = new JsonValue() { 1, 2, 3 }; Assert.That(test.Count, Is.EqualTo(3)); Assert.That(test.Contains(1), Is.True); Assert.That(test.Contains(4), Is.False); JsonValue[] values = new JsonValue[3]; test.CopyTo(values, 0); for (int i = 0; i < 3; i++) { Assert.That((int)values[i], Is.EqualTo((int)test[i])); } test.Remove(2); Assert.That(test.Contains(1), Is.True); Assert.That(test.Contains(2), Is.False); Assert.That(test.Contains(3), Is.True); Assert.That(test.IsReadOnly, Is.False); Assert.That(test.IndexOf(1), Is.EqualTo(0)); test.Insert(1, 2); Assert.That((int)test[1], Is.EqualTo(2)); Assert.That(test.Count, Is.EqualTo(3)); test.RemoveAt(1); Assert.That((int)test[1], Is.EqualTo(3)); Assert.That(test.Count, Is.EqualTo(2)); test.Clear(); Assert.That(test.Count, Is.EqualTo(0)); test.Add(5); Assert.That(test.Count, Is.EqualTo(1)); Assert.That((int)test[0], Is.EqualTo(5)); }
static JsonValue ReadObject(string json, ref int i) { var obj = new JsonValue(); obj.Type = JsonType.Object; i++; // Skip the '{' SkipWhitespace(json, ref i); if (json[i] != '}') { //ReadElements(obj, json, ref i); while (true) { SkipWhitespace(json, ref i); var key = ReadString(json, ref i); SkipWhitespace(json, ref i); if (json[i] != ':') { throw new InvalidJsonException("Expected ':' at column " + i + "!"); } ++i; // Skip the ':' SkipWhitespace(json, ref i); var value = ReadValue(json, ref i); obj.Add((string)key, value); SkipWhitespace(json, ref i); if (json[i] == ',') { i++; // Skip the ',' } else { break; } } } if (json[i] != '}') { throw new InvalidJsonException("Expected closing object token at column " + i + "!"); } i++; // Skip the '}' return obj; }
public void KVPCollection() { JsonValue test = new JsonValue() { {"one", 1}, {"two", 2}, {"three", 3}, }; test.Remove(new KeyValuePair<string, JsonValue>("one", 1)); Assert.That(test.Count, Is.EqualTo(2)); Assert.Throws<KeyNotFoundException>(() => test["one"].ToString()); KeyValuePair<string, JsonValue>[] dest = new KeyValuePair<string, JsonValue>[2]; test.CopyTo(dest, 0); for (int i = 0; i < 2; ++i) { Assert.That(test[dest[i].Key].Equals(dest[i].Value)); } Assert.That(test.Contains(new KeyValuePair<string, JsonValue>("two", 2))); test.Add(new KeyValuePair<string, JsonValue>("four", 4)); Assert.That(test.ContainsKey("four"), Is.True); Assert.That((int)test["four"], Is.EqualTo(4)); }
public void IDictionary() { JsonValue test = new JsonValue() { {"one", 1}, {"two", 2}, {"three", 3} }; Assert.That((int)test["one"], Is.EqualTo(1)); test["one"] = 5; Assert.That((int)test["one"], Is.EqualTo(5)); test["one"] = 1; var keys = test.Keys; Assert.That(keys.Count, Is.EqualTo(3)); Assert.That(keys.Contains("one"), Is.True); Assert.That(keys.Contains("two"), Is.True); Assert.That(keys.Contains("three"), Is.True); var values = test.Values; Assert.That(values.Count, Is.EqualTo(3)); var intvalues = values.Select(x => (int)x).ToArray(); Array.Sort(intvalues); for (int i = 0; i < 3; i++) { Assert.That(intvalues[i], Is.EqualTo(i + 1)); } test.Add("four", 4); Assert.That(test.Count, Is.EqualTo(4)); Assert.That((int)test["four"], Is.EqualTo(4)); JsonValue val; Assert.That(test.TryGetValue("one", out val), Is.True); Assert.That((int)val, Is.EqualTo(1)); Assert.That(test.TryGetValue("seven", out val), Is.False); Assert.That(val, Is.Null); Assert.That(test.ContainsKey("four"), Is.True); test.Remove("four"); Assert.That(test.Count, Is.EqualTo(3)); Assert.That(test.ContainsKey("four"), Is.False); }