void configureSimpleBinaryFormat(IFaserFormatter<IEnumerable<byte>> formatter) { formatter.PrepareForBinary(); var nullByte = new byte[] { 0 }; formatter.Encoder<string>((path, str) => { if (str == null) return nullByte; var bytes = Encoding.UTF8.GetBytes(str); return bytes.Concat(nullByte); }); formatter.Decoder<string>((path, encoded) => { var bytes = encoded.TakeWhile(b => b != 0).ToArray(); return Encoding.UTF8.GetString(bytes); }); formatter.Encoder<uint>((path, value) => { var bytes = new[] { (byte) ((value & 0xff000000) >> 24), (byte) ((value & 0x00ff0000) >> 16), (byte) ((value & 0x0000ff00) >> 8), (byte) ((value & 0x000000ff)) }; return bytes; }); formatter.Decoder<uint>((path, encoded) => { var bytes = encoded.Take(4).ToArray(); uint value = (uint)bytes[0] << 24 | (uint)bytes[1] << 16 | (uint)bytes[2] << 8 | bytes[3]; return value; }); formatter.Encoder<byte>((path, value) => new[] { value }); formatter.Decoder<byte>((path, encoded) => encoded.First()); }
protected void configureSimpleTextFormat(IFaserFormatter<IEnumerable<char>> formatter) { formatter .AggregateEncoder((path, instance) => new SimpleTextEncoder()) .AggregateDecoder((path, encoded) => new SimpleTextDecoder(path, encoded)); formatter.DefaultEncoder( (path, obj) => Convert.ToString(obj, CultureInfo.InvariantCulture)); formatter.DefaultDecoder( (path, encoded) => { var str = new string(encoded.ToArray()); var t = path.Member.Type; return Convert.ChangeType(str, t, CultureInfo.InvariantCulture); } ); }
public SimpleBinaryFormatter() { var f = Faser.CreateFormatter<IEnumerable<byte>>(); configureSimpleBinaryFormat(f); Formatter = f; }
public void setup() { Formatter = new SimpleTextFormatter().Formatter; }
public SimpleTextFormatter() { var f = Faser.CreateFormatter<IEnumerable<char>>(); configureSimpleTextFormat(f); Formatter = f; }