public void Empty() { var p = new PropertyNameToken(); Assert.True(p.Empty); Assert.False(p.Equals("x")); Assert.True(p.Equals(new PropertyNameToken())); }
/// <summary> /// Compares the <c>PropertyNameToken</c> to another <c>PropertyNameToken</c>. /// </summary> /// <param name="other">another name</param> /// <returns>true if the names are equal</returns> public bool Equals(PropertyNameToken other) { var otherString = other._string; if (_string != null) { return(otherString == null?other.Equals(_string) : _string.Equals(otherString)); } var otherBytes = other._asciiBytes; var otherProp = other._jsonProperty; if (_jsonProperty.HasValue) { var jp = _jsonProperty.Value; if (otherString != null) { return(jp.NameEquals(otherString)); } if (otherProp.HasValue) { return(jp.NameEquals(other._jsonProperty.Value.Name)); // inefficient, requires string allocation } return(jp.NameEquals(otherBytes)); } // We're using this._asciiBytes if (otherString != null) { return(Equals(otherString)); } if (otherProp.HasValue) { return(otherProp.Value.NameEquals(_asciiBytes)); } var len = _asciiBytes.Length; if (otherBytes.Length != len) { return(false); } for (var i = 0; i < len; i++) { if (otherBytes[i] != _asciiBytes[i]) { return(false); } } return(true); }
private void DoEqualityTests(PropertyNameToken p) { Assert.False(p.Empty); Assert.True(p.Equals("abc")); Assert.True(p == "abc"); Assert.False(p != "abc"); Assert.True(p.Equals(new PropertyNameToken("abc"))); Assert.False(p.Equals(new PropertyNameToken("abd"))); #if USE_SYSTEM_TEXT_JSON Assert.True(p.Equals(new PropertyNameToken(MakeUtf8ByteSpan("xxabczz", 2, 3)))); Assert.False(p.Equals(new PropertyNameToken(MakeUtf8ByteSpan("xxabdzz", 2, 3)))); Assert.True(p.Equals(new PropertyNameToken(MakeJsonProperty("abc")))); Assert.False(p.Equals(new PropertyNameToken(MakeJsonProperty("abd")))); #else #endif }
/// <summary> /// Advances to the next object property if any, and returns <see langword="true"/> if successful. /// </summary> /// <remarks> /// <para> /// It returns <see langword="false"/> if the <c>JReader</c> has reached the end of the object, /// or if the object was empty or null. /// </para> /// <para> /// If <c>Next</c> returns <see langword="true"/>, you can then use <see cref="Name"/> to check /// the name of the property, and use <see cref="JReader"/> methods such as <see cref="JReader.Bool"/> /// to read the element value. If you do not care about the value, simply calling <c>Next</c> again /// without calling a <c>JReader</c> method will discard the value. /// </para> /// </remarks> /// <returns><see langword="true"/> if there is a next object property</returns> public bool Next(ref JReader reader) { if (!_defined) { return(false); } _name = reader.ObjectNext(!_afterFirst); _afterFirst = true; if (!_name.Empty) { if (_requiredProperties != null) { for (int i = 0; i < _requiredProperties.Length; i++) { if (_name.Equals(_requiredProperties[i])) { _foundProperties[i] = true; break; } } } return(true); } if (_requiredProperties != null) { for (int i = 0; i < _requiredProperties.Length; i++) { if (!_foundProperties[i]) { throw new RequiredPropertyException(_requiredProperties[i], reader.LastPos); } } } return(false); }