lorenzodonini / ocpp-go

Open Charge Point Protocol implementation in Go
MIT License
277 stars 126 forks source link

Dealing with non conforming json ocpp messages #190

Open rogeralsing opened 1 year ago

rogeralsing commented 1 year ago

I've seen vendors here in Sweden that send OCPP messages that are slightly non-conforming to the spec. e.g. in some cases where the spec says it should be a number, they might send a quoted string, containing a number. Or sending "" for 0.

They are the ones not following the spec, but in the harsh reality where you can't do much about how the vendor implemented the spec. how would you deal with this using ocpp-go ?

lorenzodonini commented 1 year ago

Short version: you can't, because the lib is meant to be spec-conform 😅

Long version: if you knew beforehand, which specific messages are not conform, you could fork the lib and write custom JSON unmarshaling functions that handle those edge cases. Or you could change some specific fields to an interface{} and handle those fields yourself accordingly within the callbacks, at the cost of extra type assertions and conditional switches. This will require you to maintain your own fork though.

In general, since Go is strongly typed, incoming values cannot be treated like you would in a duck-typed language such as Python. Any parsing and validation is strictly tied to a type and will fail if a different type is received.

tvand commented 1 year ago

See my approach for a similar case in https://github.com/lorenzodonini/ocpp-go/pull/240