-
Notifications
You must be signed in to change notification settings - Fork 258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create GeoJSON serializer #830
Conversation
As an alternative. You can register a serializer or a parser directly on the fly. Here is a an example using xml. library(plumber)
library(xml2)
serializer_xml <- function(..., type = "application/xml") {
serializer_content_type(type, function(val) {
as.character(xml2::as_xml_document(val, ...))
})
}
register_serializer("xml", serializer_xml)
parser_xml <- function(...) {
parser_text(function(txt_value) {
xml2::as_list(xml2::read_xml(txt_value, ...))
})
}
register_parser("xml", parser_xml, fixed = c("text/xml", "application/xml"), regex = "/xml$")
#* @post /score/xml
#* @serializer xml
#* @parser xml
#* @parser multi
#* @param info:file
#* @param report:file
#* @param debug
function(info, report, req) {
process_input(info, report)
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notes: https://datatracker.ietf.org/doc/html/rfc7946#section-12 defines application/geo+json
as the standard Content-Type for geojson data
Thank you @JosiahParry ! Is there a way to close the round trip loop by adding a geojson parser? (Be able to both parse and serialize geojson information). @meztez's comment above gives a quick example on how to implement a parser. If you run into more serializers/parsers that you believe are commonly used, please feel free to add another PR 😄 |
@schloerke thanks for the comments! |
geojsonsf use a different R json library. I wonder if this could be avoided to reduce plumber depencies? |
@meztez , do you think these codes should live within the geojson pkg? I don't remember the setup for |
@meztez No libraries were added to |
Great! In that case, should we prepend call to geojsonsf function with a requireNamespace check? @JosiahParry thanks a lot for this contribution |
Pass a non valid geojson value to the serializer.
b8e2763
to
d880abf
Compare
- Update namespace w/ devtools::document() - Update news under "New features" - Move check for `geojsonsf` package to top of function - Add `application/vdn.geo+json` as valid content type
All requested changes should be addressed in most recent commit. |
Exciting!!! Thanks all for your guidance :) |
* main: Fix Content-Length header on HTTP responses which forbid it (#760) Generate docs and fix existing tests (#860) decode static URI before serving (#754) Update feather serializer; Add parquet serializer (#849) remove author in pkgdown; have website build again Fix plumber website. Use tidytemplate (#851) Runtime Vignette typo (#848) GHA v2 (#842) master -> main (#839) Create GeoJSON serializer and parser (#830) Add details about named functions as router modifiers (#824) Parser params should be in a `list()` (#827) Test on ubuntu 18/20 and use relative R versions (#828) Shared secret rejections are not errors (#809) Fix tag endpoint block annotation spelling to `@tag` (#800) Add ORCID info (#794) Add blog post link to website (#792) Use serializer Content-Type header for PlumberEndpoint API spec (#789) Use dev version (#790) v1.1.0 (#752)
Pull Request
This pull requests implements a geojson serializer. The serializer utilizes geojsonsf. The serializer only supports objects of class
sf
andsfc
.Please note that
devtools::check()
fails due tohookable.R#3
and it cannot find the namespace. As such I cannot provide check results without fiddling with code that is not mine.No code was modified, only added. As such there should be no breaking features or breaking backwards compatibility.
At present, the only way to provide geojson—that I am aware of—via plumber API is to utilize sf to write to a temporary file. read that tempfile via
readLines()
and use acat
serializer to return the exact json that was written by sf. This serializer removes the need to create hacky solutions for those who would like to return geojson from a plumber API.PR task list:
devtools::document()