In this example we convert to the time data type: SELECT CAST('' AS time) AS Result Here we convert to the smalldatetime data type: SELECT CAST('' AS smalldatetime) AS Result
Result: +-+Įxample 5 – Convert to smalldatetime Data Type In this example we convert to the datetimeoffset data type: SELECT CAST('' AS datetimeoffset) AS Result Here we convert to the datetime2 data type: SELECT CAST('' AS datetime2) AS Result Įxample 4 – Convert to datetimeoffset Data Type You can also specify the time along with the date: SELECT CAST('2pm ' AS datetime) AS Result Įxample 3 – Convert to datetime2 Data Type In this example we convert to the datetime data type: SELECT CAST('' AS datetime) AS Result Result: Conversion failed when converting date and/or time from character string.Įxample 2 – Convert to datetime Data Type Just like with CONVERT() the string needs to be in a style that’s able to be converted to the specified data type, otherwise you’ll get an error: SELECT CAST('Homer' AS date) AS Result In this example, we convert the string into a date data type (we specify this by using date as the second argument). length An optional integer that specifies the length of the target data type. This includes xml, bigint, and sql_variant.
These arguments are defined as follows: expression Any valid expression. The syntax goes like this: CAST ( expression AS data_type ) This article contains examples using the CAST() function. So if you need to convert a string to a date/time value, you can use the CONVERT() function or the CAST() function.Īnd if you get an error while using those two functions, the PARSE() function might be just what you need. This function works almost the same as the CONVERT() function, except that the syntax is slightly different (and CAST() doesn’t accept the style argument).
Throw new InvalidOperationException("tiles != null & tiles.GetLength(0) != value.GetLength(0) || tiles.GetLength(1) != value.In SQL Server, you can use the CAST() function to convert an expression of one data type to another. If (tiles != null & tiles.GetLength(0) != value.GetLength(0) || tiles.GetLength(1) != value.GetLength(1)) Note that the parameter name discoveredTiles must be the same as the corresponding serialized property name.Īdd a private or protected setter for discoveredTiles and mark it with : Throw new InvalidOperationException("tiles.GetLength(0) != discoveredTiles.GetLength(0) || tiles.GetLength(1) != discoveredTiles.GetLength(1)") If (tiles.GetLength(0) != discoveredTiles.GetLength(0) || tiles.GetLength(1) != discoveredTiles.GetLength(1)) Ensure the tiles and discoveredTiles arrays have the same sizes If (tiles = null || discoveredTiles = null) Protected WorldInstance(WorldTile tiles, bool discoveredTiles) You have two workarounds for this problem:Īdd a protected constructor to WorldInstance that includes a bool discoveredTiles argument and mark it with, for instance: (Note that the tiles property will get deserialized by passing it to the constructor, matching the JSON property name to the constructor argument name.)įor a 1d array property, the exception thrown explains this problem: System.NotSupportedException: Collection was of a fixed size.Īt `1.Add(T item) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\Utilities\CollectionWrapper.cs:line 76Īt `1.(Object value) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\Utilities\CollectionWrapper.cs:line 194Īt .CreateObjectUsingCreatorWithParameters(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ObjectConstructor`1 creator, String id) in C:\Development\Releases\Json\Working\Newtonsoft.Json\Working-Signed\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 2030īut for a 2d array the less illuminating InvalidCastException is thrown. This is currently not implemented even if the array is pre-allocated and has the same length as the JSON array, since Json.NET will try to add deserialized items to the pre-allocated array, which is of course impossible. Your problem is that you are trying to make Json.NET deserialize a get-only array property discoveredTiles, as is shown in the following simplified version of your class: public class WorldInstance