Created
December 6, 2013 19:32
-
-
Save aneves/7830776 to your computer and use it in GitHub Desktop.
Minimalist sample: ORMLite does not properly serialize structs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.Linq; | |
using System.Threading; | |
using ServiceStack; | |
using ServiceStack.OrmLite; | |
using ServiceStack.Text; | |
namespace ORMLiteSerializationTests | |
{ | |
class SerializationOfStructsIsNotCalled | |
{ | |
static void Main(string[] args) | |
{ | |
var culture = CultureInfo.GetCultureInfo("pt-PT"); | |
Thread.CurrentThread.CurrentCulture = culture; | |
var thing = new Thing(12.6f, 10.9f); | |
Console.WriteLine("Thing, current culture: {0}", thing.ToString()); | |
Console.WriteLine("Thing, invariant culture: {0}", thing.ToString(CultureInfo.InvariantCulture)); | |
Console.WriteLine("Thing, from Jsv: {0}", thing.ToJsv()); | |
JsConfig<Thing>.SerializeFn = t => | |
{ | |
Console.WriteLine(">> serializing {0}", t); | |
return t.ToString(CultureInfo.InvariantCulture); | |
}; | |
JsConfig<Thing>.DeSerializeFn = str => | |
{ | |
Console.WriteLine(">> deserializing {0}", str); | |
return Thing.Parse(str, CultureInfo.InvariantCulture); | |
}; | |
JsConfig<Thing>.TreatValueAsRefType = true; | |
OrmLiteConfig.DialectProvider = SqliteDialect.Provider; | |
var factory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider, false); | |
using (var conn = factory.OpenDbConnection()) | |
{ | |
conn.DropAndCreateTable<Box>(); | |
conn.Insert(new[] | |
{ | |
new Box{ Name="A", Content = new Thing(10f, 35f)}, | |
new Box{ Name="B", Content = new Thing(12.3f, 45.6f)}, | |
new Box{ Name="C", Content = new Thing(1f, 2f)}, | |
new Box{ Name="D", Content = new Thing(3f, -10f)}, | |
new Box{ Name="E", Content = new Thing(5f, 3f)}, | |
}); | |
var b = OrmLiteReadConnectionExtensions.Where<Box>(conn, "Content", "12.3;45.6"); | |
var items = conn.Where<Box>(new { Name = "A" }); | |
if (!items.Any()) | |
{ | |
Console.WriteLine("Found nothing..."); | |
} | |
else | |
{ | |
foreach (var item in items) | |
{ | |
Console.WriteLine("Found this: {0}", item); | |
} | |
} | |
} | |
} | |
} | |
public class Box | |
{ | |
public string Name { get; set; } | |
public Thing Content { get; set; } | |
public override string ToString() | |
{ | |
return ToString(CultureInfo.CurrentCulture); | |
} | |
public string ToString(CultureInfo culture) | |
{ | |
return string.Format(culture, "Box[{0}: {1}]", Name, Content); | |
} | |
} | |
public struct Thing | |
{ | |
public float A { get; set; } | |
public float B { get; set; } | |
public Thing(float a, float b) | |
: this() | |
{ | |
A = a; | |
B = b; | |
} | |
public static Thing Parse(string value) | |
{ | |
return Parse(value, CultureInfo.InvariantCulture); | |
} | |
public static Thing Parse(string value, CultureInfo culture) | |
{ | |
string separator = culture.TextInfo.ListSeparator; | |
var values = value.Split(new[] { separator }, StringSplitOptions.None); | |
float a; | |
float b; | |
if (values.Length != 2 | |
|| !float.TryParse(values[0], out a) | |
|| !float.TryParse(values[1], out b) | |
) | |
{ | |
string message = "Could not parse value, it is malformed."; | |
Console.WriteLine("{0} ({1)", message, value); | |
throw new ArgumentException(message, "value"); | |
} | |
return new Thing(a, b); | |
} | |
public override string ToString() | |
{ | |
return ToString(CultureInfo.CurrentCulture); | |
} | |
public string ToString(CultureInfo culture) | |
{ | |
string separator = culture.TextInfo.ListSeparator; | |
return string.Format(culture, "{0}{1}{2}", A, separator, B); | |
} | |
} | |
} |
To see it working as expected, change line 16 from Thread.CurrentThread.CurrentCulture = culture;
to Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
.
There is an error lines 103, 104. The corrected version should read:
|| !float.TryParse(values[0], NumberStyles.Float | NumberStyles.AllowThousands, culture, out a)
|| !float.TryParse(values[1], NumberStyles.Float | NumberStyles.AllowThousands, culture, out b)
Otherwise this would fail parsing using the supplied culture.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uses the "OrmLite.Sqlite.64bit" package from Nuget: ServiceStack.OrmLite.Sqlite64 v4.0.3 (latest).