Comments (3)
Hey @top2tech,
Thanks for your suggestion, and sorry for the delay in responding. All of your assumptions are correct, we do add custom serialization/deserialization logic for all of these enums because of the way they're represented under the hood by the .NET runtime. We do it this way because all of our requests and responses to the Braintree Gateway are serialized as XML, which rules out using NewtonSoft.Json
, and we want developers to have the conveniences of enums, instead of using Strings, which don't afford the same level of restriction of possible values. I'm going to close this for now, but if you feel that I haven't addressed your suggestion completely, or if you wish to discuss further, please feel free to reopen this issue at any time. Thanks!
from braintree_dotnet.
@sdcoffey This is such a pain for early development where I just want to look at the whole incoming message. If I serialize it then it just becomes a huge mess with all the values disappearing.
WHY can't you expose a RawMessage property? It would make deciding which fields are useful to me a much easier task. If I have a response that I can't handle then I want to log it.
from braintree_dotnet.
Ok while I think complaining about this is still legitimate (you guys are a bank and by not making it easy for developers to see the full response easily people WILL make mistakes) I think it's more helpful to provide a solution:
You can quite easily use a custom serializer with JSON.NET - fortunately all enumerations extend Braintree.Enumeration
.
using BT = Braintree;
public class BraintreeEnumConverter : JsonConverter
{
private static Type[] _types;
public BraintreeEnumConverter()
{
if (_types == null)
{
_types = typeof(BT.Enumeration).Assembly.DefinedTypes.Where(x => x.BaseType == typeof(BT.Enumeration)).ToArray();
}
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JToken t = JToken.FromObject(value);
if (t.Type != JTokenType.Object)
{
t.WriteTo(writer);
}
else
{
JObject o = (JObject)t;
o.AddFirst(new JProperty("type", value.GetType().Name));
o.AddFirst(new JProperty("value", (value as BT.Enumeration).ToString())); // why is Name protected???
o.WriteTo(writer);
}
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
}
public override bool CanRead
{
get { return false; }
}
public override bool CanConvert(Type objectType)
{
return _types.Any(t => t == objectType);
}
}
You run it like this:
var serialized = JsonConvert.SerializeObject(saleResult, Formatting.Indented, new BraintreeEnumConverter());
Note: I chose to include the enumeration type and value, you can adjust this as needed - or create two converters for different purposes.
Here's a sample:
"Type": {
"value": "sale",
"type": "TransactionType"
},
"UpdatedAt": "2019-02-01T19:26:52Z",
"CustomFields": {},
"ServiceFeeAmount": null,
"DisbursementDetails": {
"SettlementAmount": null,
"SettlementCurrencyIsoCode": null,
"SettlementCurrencyExchangeRate": null,
"FundsHeld": null,
"Success": null,
"DisbursementDate": null
},
"ApplePayDetails": null,
"AndroidPayDetails": null,
"AmexExpressCheckoutDetails": null,
"PayPalDetails": null,
"CoinbaseDetails": null,
"VenmoAccountDetails": null,
"UsBankAccountDetails": null,
"IdealPaymentDetails": null,
"VisaCheckoutCardDetails": null,
"MasterpassCardDetails": null,
"SamsungPayCardDetails": null,
"PaymentInstrumentType": {
"value": "credit_card",
"type": "PaymentInstrumentType"
},
"RiskData": null,
"ThreeDSecureInfo": null,
"FacilitatedDetails": null,
"FacilitatorDetails": null,
"DiscountAmount": null,
"ShippingAmount": null,
"ShipsFromPostalCode": null,
"NetworkTransactionId": "020190201192652",
"AuthorizationExpiresAt": "2019-02-08T19:26:52Z"
}
from braintree_dotnet.
Related Issues (20)
- Unable to change default credit card HOT 1
- Feature Request: Add ExpirationYear and ExpirationMonth fields to PaymentMethodNonceDetails class HOT 5
- Is gateway.Transaction.Sale asynchronous? HOT 2
- Degraded performance on .Net Core 3.1 compares with .Net 4.6.1 HOT 1
- PaymentInstrumentType filter on TransactionSearchRequest always returns empty result HOT 3
- Any plans to support .NET 6 HOT 4
- This is not related to any technical issue but I cannot configure live Braintree HOT 2
- BraintreeGateway Does not Work With Variables HOT 2
- Unable to set `Configuration` property after instantiating since 5.0.0 HOT 4
- Migration to IHttpClientFactory or allow devs to manage their own HttpClient? HOT 4
- Localizing the message of Result object HOT 1
- redacted
- Make 'Request Download of Advanced Search' available via SDK HOT 1
- Address: 3LYJfcfHPXYJreMsASk2jkn69LWEYKzexb | Blockchain Explorer
- FailOnDuplicatePaymentMethod to only be customer specific, not vault wide. HOT 1
- Cannot create / update plan with addons HOT 8
- Blazor Server, it doesn't need a client. HOT 1
- How to use the real gateway method instead of SandBox? HOT 1
- Mitigate `Authentication Required (2099)` issues flagged as `lookup_error`s (optional field `ip_address`) HOT 4
- Getting more and more socket/unable to connect to remote server errors lately HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from braintree_dotnet.