BooleanSerializer.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #if !NO_RUNTIME
  2. using System;
  3. namespace ProtoBuf.Serializers
  4. {
  5. sealed class BooleanSerializer : IProtoSerializer
  6. {
  7. static readonly Type expectedType = typeof(bool);
  8. public BooleanSerializer(ProtoBuf.Meta.TypeModel model) { }
  9. public Type ExpectedType => expectedType;
  10. public void Write(object value, ProtoWriter dest)
  11. {
  12. ProtoWriter.WriteBoolean((bool)value, dest);
  13. }
  14. public object Read(object value, ProtoReader source)
  15. {
  16. Helpers.DebugAssert(value == null); // since replaces
  17. return source.ReadBoolean();
  18. }
  19. bool IProtoSerializer.RequiresOldValue => false;
  20. bool IProtoSerializer.ReturnsValue => true;
  21. #if FEAT_COMPILER
  22. void IProtoSerializer.EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  23. {
  24. ctx.EmitBasicWrite("WriteBoolean", valueFrom);
  25. }
  26. void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  27. {
  28. ctx.EmitBasicRead("ReadBoolean", ExpectedType);
  29. }
  30. #endif
  31. }
  32. }
  33. #endif