json.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. This file is part of go-ethereum
  3. go-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. go-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package rpc
  15. import (
  16. "encoding/json"
  17. "io"
  18. "net/http"
  19. )
  20. type jsonWrapper struct{}
  21. func (self jsonWrapper) Send(writer io.Writer, v interface{}) (n int, err error) {
  22. var payload []byte
  23. payload, err = json.Marshal(v)
  24. if err != nil {
  25. jsonlogger.Fatalln("Error marshalling JSON", err)
  26. return 0, err
  27. }
  28. jsonlogger.Infof("Sending payload: %s", payload)
  29. return writer.Write(payload)
  30. }
  31. func (self jsonWrapper) ParseRequestBody(req *http.Request) (RpcRequest, error) {
  32. var reqParsed RpcRequest
  33. // Convert JSON to native types
  34. d := json.NewDecoder(req.Body)
  35. // d.UseNumber()
  36. defer req.Body.Close()
  37. err := d.Decode(&reqParsed)
  38. if err != nil {
  39. jsonlogger.Errorln("Error decoding JSON: ", err)
  40. return reqParsed, err
  41. }
  42. jsonlogger.DebugDetailf("Parsed request: %s", reqParsed)
  43. return reqParsed, nil
  44. }
  45. var JSON jsonWrapper