public void ReturnNullIfSerializingNull() { Serializer <string> serializer = new SqlVarcharSerializer(); byte[] actualSerializedValue = serializer.Serialize(null); Assert.Null(actualSerializedValue); }
private static (TypeMarker, byte[]) Serialize(JToken propertyValue) { SqlSerializerFactory sqlSerializerFactory = new SqlSerializerFactory(); // UTF-8 encoding. SqlVarcharSerializer sqlVarcharSerializer = new SqlVarcharSerializer(size: -1, codePageCharacterEncoding: 65001); switch (propertyValue.Type) { case JTokenType.Undefined: Debug.Assert(false, "Undefined value cannot be in the JSON"); return(default, null); case JTokenType.Null: Debug.Assert(false, "Null type should have been handled by caller"); return(TypeMarker.Null, null); case JTokenType.Boolean: return(TypeMarker.Boolean, sqlSerializerFactory.GetDefaultSerializer <bool>().Serialize(propertyValue.ToObject <bool>())); case JTokenType.Float: return(TypeMarker.Double, sqlSerializerFactory.GetDefaultSerializer <double>().Serialize(propertyValue.ToObject <double>())); case JTokenType.Integer: return(TypeMarker.Long, sqlSerializerFactory.GetDefaultSerializer <long>().Serialize(propertyValue.ToObject <long>())); case JTokenType.String: return(TypeMarker.String, sqlVarcharSerializer.Serialize(propertyValue.ToObject <string>())); case JTokenType.Array: return(TypeMarker.Array, sqlVarcharSerializer.Serialize(propertyValue.ToString())); case JTokenType.Object: return(TypeMarker.Object, sqlVarcharSerializer.Serialize(propertyValue.ToString())); default: throw new InvalidOperationException($" Invalid or Unsupported Data Type Passed : {propertyValue.Type}"); } }
public void SerializeTheSameAsSqlServer(string plaintext, int size, int codepage) { SqlVarcharSerializer serializer = new SqlVarcharSerializer(size, codepage); byte[] serializedPlaintext = serializer.Serialize(plaintext); byte[] expectedCiphertext = deterministicEncryptionAlgorithm.Encrypt(serializedPlaintext); Database.Insert(new SqlParameter("@parameter", SqlDbType.VarChar, size) { Value = plaintext }); byte[] actualCiphertext = Database.SelectCiphertext(SqlDbType.VarChar); Assert.Equal(expectedCiphertext, actualCiphertext); }