William Cheng b6576d1173
[R] Update fromJSON to handle additional properties (#13331)
* update fromJSON to handle additional properties

* update RoxygenNote to newer version
2022-09-02 09:46:56 +08:00

249 lines
7.2 KiB
R

#' Create a new BasquePig
#'
#' @description
#' BasquePig Class
#'
#' @docType class
#' @title BasquePig
#' @description BasquePig Class
#' @format An \code{R6Class} generator object
#' @field className character
#' @field color character
#' @field _field_list a list of fields list(character)
#' @field additional_properties additional properties list(character) [optional]
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
BasquePig <- R6::R6Class(
"BasquePig",
public = list(
`className` = NULL,
`color` = NULL,
`_field_list` = c("className", "color"),
`additional_properties` = list(),
#' Initialize a new BasquePig class.
#'
#' @description
#' Initialize a new BasquePig class.
#'
#' @param className className
#' @param color color
#' @param additional_properties additonal properties (optional)
#' @param ... Other optional arguments.
#' @export
initialize = function(
`className`, `color`, additional_properties = NULL, ...
) {
if (!missing(`className`)) {
stopifnot(is.character(`className`), length(`className`) == 1)
self$`className` <- `className`
}
if (!missing(`color`)) {
stopifnot(is.character(`color`), length(`color`) == 1)
self$`color` <- `color`
}
if (!is.null(additional_properties)) {
for (key in names(additional_properties)) {
self$additional_properties[[key]] <- additional_properties[[key]]
}
}
},
#' To JSON string
#'
#' @description
#' To JSON String
#'
#' @return BasquePig in JSON format
#' @export
toJSON = function() {
BasquePigObject <- list()
if (!is.null(self$`className`)) {
BasquePigObject[["className"]] <-
self$`className`
}
if (!is.null(self$`color`)) {
BasquePigObject[["color"]] <-
self$`color`
}
for (key in names(self$additional_properties)) {
BasquePigObject[[key]] <- self$additional_properties[[key]]
}
BasquePigObject
},
#' Deserialize JSON string into an instance of BasquePig
#'
#' @description
#' Deserialize JSON string into an instance of BasquePig
#'
#' @param input_json the JSON input
#' @return the instance of BasquePig
#' @export
fromJSON = function(input_json) {
this_object <- jsonlite::fromJSON(input_json)
if (!is.null(this_object$`className`)) {
self$`className` <- this_object$`className`
}
if (!is.null(this_object$`color`)) {
self$`color` <- this_object$`color`
}
# process additional properties/fields in the payload
for (key in names(this_object)) {
if (!(key %in% self$`_field_list`)) { # json key not in list of fields
self$additional_properties[[key]] <- this_object[[key]]
}
}
self
},
#' To JSON string
#'
#' @description
#' To JSON String
#'
#' @return BasquePig in JSON format
#' @export
toJSONString = function() {
jsoncontent <- c(
if (!is.null(self$`className`)) {
sprintf(
'"className":
"%s"
',
self$`className`
)
},
if (!is.null(self$`color`)) {
sprintf(
'"color":
"%s"
',
self$`color`
)
}
)
jsoncontent <- paste(jsoncontent, collapse = ",")
json_string <- as.character(jsonlite::minify(paste("{", jsoncontent, "}", sep = "")))
json_obj <- jsonlite::fromJSON(json_string)
for (key in names(self$additional_properties)) {
json_obj[[key]] <- self$additional_properties[[key]]
}
json_string <- as.character(jsonlite::minify(jsonlite::toJSON(json_obj, auto_unbox = TRUE, digits = NA)))
},
#' Deserialize JSON string into an instance of BasquePig
#'
#' @description
#' Deserialize JSON string into an instance of BasquePig
#'
#' @param input_json the JSON input
#' @return the instance of BasquePig
#' @export
fromJSONString = function(input_json) {
this_object <- jsonlite::fromJSON(input_json)
self$`className` <- this_object$`className`
self$`color` <- this_object$`color`
# process additional properties/fields in the payload
for (key in names(this_object)) {
if (!(key %in% self$`_field_list`)) { # json key not in list of fields
self$additional_properties[[key]] <- this_object[[key]]
}
}
self
},
#' Validate JSON input with respect to BasquePig
#'
#' @description
#' Validate JSON input with respect to BasquePig and throw an exception if invalid
#'
#' @param input the JSON input
#' @export
validateJSON = function(input) {
input_json <- jsonlite::fromJSON(input)
# check the required field `className`
if (!is.null(input_json$`className`)) {
stopifnot(is.character(input_json$`className`), length(input_json$`className`) == 1)
} else {
stop(paste("The JSON input `", input, "` is invalid for BasquePig: the required field `className` is missing."))
}
# check the required field `color`
if (!is.null(input_json$`color`)) {
stopifnot(is.character(input_json$`color`), length(input_json$`color`) == 1)
} else {
stop(paste("The JSON input `", input, "` is invalid for BasquePig: the required field `color` is missing."))
}
},
#' To string (JSON format)
#'
#' @description
#' To string (JSON format)
#'
#' @return String representation of BasquePig
#' @export
toString = function() {
self$toJSONString()
},
#' Return true if the values in all fields are valid.
#'
#' @description
#' Return true if the values in all fields are valid.
#'
#' @return true if the values in all fields are valid.
#' @export
isValid = function() {
# check if the required `className` is null
if (is.null(self$`className`)) {
return(FALSE)
}
# check if the required `color` is null
if (is.null(self$`color`)) {
return(FALSE)
}
TRUE
},
#' Return a list of invalid fields (if any).
#'
#' @description
#' Return a list of invalid fields (if any).
#'
#' @return A list of invalid fields (if any).
#' @export
getInvalidFields = function() {
invalid_fields <- list()
# check if the required `className` is null
if (is.null(self$`className`)) {
invalid_fields["className"] <- "Non-nullable required field `className` cannot be null."
}
# check if the required `color` is null
if (is.null(self$`color`)) {
invalid_fields["color"] <- "Non-nullable required field `color` cannot be null."
}
invalid_fields
}
),
# Lock the class to prevent modifications to the method or field
lock_class = TRUE
)
# Unlock the class to allow modifications of the method or field
BasquePig$unlock()
#' Print the object
#'
#' @description
#' Print the object
#'
#' @export
BasquePig$set("public", "print", function(...) {
print(jsonlite::prettify(self$toJSONString()))
invisible(self)
})
# Lock the class to prevent modifications to the method or field
BasquePig$lock()