Created
June 7, 2019 09:57
-
-
Save joao-r-reis/da1ca4f982f3351444699316ec975ba9 to your computer and use it in GitHub Desktop.
Example of a custom type converter to serialize objects to json strings.
This file contains 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
// | |
// Copyright (C) DataStax, Inc. | |
// | |
// Please see the license for details: | |
// http://www.datastax.com/terms/datastax-dse-driver-license-terms | |
// | |
using System; | |
using System.ComponentModel; | |
using System.Reflection; | |
using Dse; | |
using Dse.Data.Linq; | |
using Dse.Mapping; | |
using Newtonsoft.Json; | |
namespace myApp | |
{ | |
public static class Extensions | |
{ | |
public static T FromJson<T>(this string obj) | |
{ | |
return JsonConvert.DeserializeObject<T>(obj); | |
} | |
public static string ToJson<T>(this T obj) | |
{ | |
return JsonConvert.SerializeObject(obj); | |
} | |
} | |
public class Programv3 | |
{ | |
public static void MainEntry(string[] args) | |
{ | |
var ks = "programv3"; | |
var session = DseCluster.Builder().AddContactPoint("127.0.0.1").Build().Connect(); | |
session.CreateKeyspaceIfNotExists(ks); | |
session.ChangeKeyspace(ks); | |
session.Execute(@"CREATE TABLE IF NOT EXISTS poco ( id BigInt PRIMARY KEY, data BigInt, inner_poco text )"); | |
var mapConfig = new MappingConfiguration().ConvertTypesUsing(new JsonStringTypeConverter()); | |
var table = new Table<Poco>(session, mapConfig); | |
table.Insert(new Poco { Data = 123, Id = 1, InnerPoco = new InnerPoco { One = 1, Two = "two" } }).Execute(); | |
var elem = table.First(t => t.Id == 1).Execute(); | |
Console.WriteLine(JsonConvert.SerializeObject(elem)); | |
Console.WriteLine("Done"); | |
Console.ReadLine(); | |
} | |
public class TableColumnTypeConversionRequiredAttribute : Attribute | |
{ | |
} | |
[Dse.Mapping.Attributes.Table("poco")] | |
public class Poco | |
{ | |
[Dse.Mapping.Attributes.PartitionKey] | |
[Dse.Mapping.Attributes.Column("id")] | |
public long Id { get; set; } | |
[Dse.Mapping.Attributes.Column("data")] | |
public long Data { get; set; } | |
[Dse.Mapping.Attributes.Column("inner_poco", Type = typeof(string))] | |
public InnerPoco InnerPoco { get; set; } | |
} | |
[TableColumnTypeConversionRequired] | |
public class InnerPoco | |
{ | |
public long One { get; set; } | |
public string Two { get; set; } | |
} | |
public class JsonStringTypeConverter : Dse.Mapping.TypeConversion.TypeConverter | |
{ | |
protected override Func<TDatabase, TPoco> GetUserDefinedFromDbConverter<TDatabase, TPoco>() | |
{ | |
TPoco JsonDeserialize(TDatabase db) => db.ToString().FromJson<TPoco>(); | |
return CanConvert<TDatabase, TPoco>() ? (Func<TDatabase, TPoco>) JsonDeserialize : null; | |
} | |
protected override Func<TPoco, TDatabase> GetUserDefinedToDbConverter<TPoco, TDatabase>() | |
{ | |
TDatabase JsonSerialize(TPoco poco) => (TDatabase)TypeDescriptor.GetConverter(typeof(TDatabase)).ConvertFrom(poco.ToJson()); | |
return CanConvert<TDatabase, TPoco>() ? (Func<TPoco, TDatabase>) JsonSerialize : null; | |
} | |
private bool CanConvert<TDatabaseType, TPocoType>() | |
{ | |
return typeof(TDatabaseType) == typeof(string) && | |
typeof(TPocoType).GetCustomAttribute(typeof(TableColumnTypeConversionRequiredAttribute)) != null; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment