OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015-2016, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using Grpc.Core.Utils; |
| 36 |
| 37 namespace Grpc.Core |
| 38 { |
| 39 /// <summary> |
| 40 /// Encapsulates the logic for serializing and deserializing messages. |
| 41 /// </summary> |
| 42 public class Marshaller<T> |
| 43 { |
| 44 readonly Func<T, byte[]> serializer; |
| 45 readonly Func<byte[], T> deserializer; |
| 46 |
| 47 /// <summary> |
| 48 /// Initializes a new marshaller. |
| 49 /// </summary> |
| 50 /// <param name="serializer">Function that will be used to serialize mes
sages.</param> |
| 51 /// <param name="deserializer">Function that will be used to deserialize
messages.</param> |
| 52 public Marshaller(Func<T, byte[]> serializer, Func<byte[], T> deserializ
er) |
| 53 { |
| 54 this.serializer = GrpcPreconditions.CheckNotNull(serializer, "serial
izer"); |
| 55 this.deserializer = GrpcPreconditions.CheckNotNull(deserializer, "de
serializer"); |
| 56 } |
| 57 |
| 58 /// <summary> |
| 59 /// Gets the serializer function. |
| 60 /// </summary> |
| 61 public Func<T, byte[]> Serializer |
| 62 { |
| 63 get |
| 64 { |
| 65 return this.serializer; |
| 66 } |
| 67 } |
| 68 |
| 69 /// <summary> |
| 70 /// Gets the deserializer function. |
| 71 /// </summary> |
| 72 public Func<byte[], T> Deserializer |
| 73 { |
| 74 get |
| 75 { |
| 76 return this.deserializer; |
| 77 } |
| 78 } |
| 79 } |
| 80 |
| 81 /// <summary> |
| 82 /// Utilities for creating marshallers. |
| 83 /// </summary> |
| 84 public static class Marshallers |
| 85 { |
| 86 /// <summary> |
| 87 /// Creates a marshaller from specified serializer and deserializer. |
| 88 /// </summary> |
| 89 public static Marshaller<T> Create<T>(Func<T, byte[]> serializer, Func<b
yte[], T> deserializer) |
| 90 { |
| 91 return new Marshaller<T>(serializer, deserializer); |
| 92 } |
| 93 |
| 94 /// <summary> |
| 95 /// Returns a marshaller for <c>string</c> type. This is useful for test
ing. |
| 96 /// </summary> |
| 97 public static Marshaller<string> StringMarshaller |
| 98 { |
| 99 get |
| 100 { |
| 101 return new Marshaller<string>(System.Text.Encoding.UTF8.GetBytes
, |
| 102 System.Text.Encoding.UTF8.GetStrin
g); |
| 103 } |
| 104 } |
| 105 } |
| 106 } |
OLD | NEW |