mps.json.encoderequest
Convert MATLAB data in a server request to JSON text using MATLAB Production Server JSON schema
Description
encodes the request that is input to the deployed MATLAB® function using JSON schema for MATLAB
Production Server™. It builds a server request that includes MATLAB variables and
options, such as text
= mps.json.encoderequest(rhs
)'Nargout'
and 'OutputFormat'
, that are needed to make a call to MATLAB
Production Server.
specifies additional options with one or more name-value pair arguments for specific
input cases. text
= mps.json.encoderequest(rhs
,Name,Value
)
Examples
Write MATLAB Production Server Payload
mps.json.encoderequest({[1 2 3 4]})
ans = '{"rhs":[[[1,2,3,4]]],"nargout":1,"outputFormat":{"mode":"small","nanType":"string"}}'
Write MATLAB Production Server Payload, and Set Output Parameters
rhs = {['Red'], [15], [1 3; 5 7], ['Green']}; mps.json.encoderequest(rhs, 'Nargout', 3, 'OutputFormat', 'large')
ans = '{"rhs":["Red",15,[[1,3],[5,7]],"Green"],"nargout":3,"outputFormat":{"mode":"large","nanType":"string"}}'
Write a MATLAB Function as MATLAB Production Server Payload
Use the MATLAB function horzcat
that horizontally
concatenates two matrices.
a = [1 2; 5 6]; b = [3 4; 7 8]; mps.json.encoderequest({horzcat(a,b)})
ans = '{"rhs":[[[1,2,3,4],[5,6,7,8]]],"nargout":1,"outputFormat":{"mode":"small","nanType":"string"}}'
Read Response from a sortstudent
Function Deployed on MATLAB
Production Server
Execute mps.json.encoderequest
and
mps.json.decoderesponse
to call a function deployed on
MATLAB
Production Server using webwrite
. In this case, student names and
their corresponding scores are deployed to MATLAB
Production Server to the sortstudents
function that sorts
students based on their scores. The result returned is the equivalent to calling
the function sortstudents(struct('name', 'Ed', 'score', 83),
struct('name', 'Toni', 'score', 91))
from MATLAB.
Assume that there is a deployable archive studentapp
that
contains a MATLAB function sortstudents
deployed to the
server.
Note
Ensure that you make calls to the test server using a different
process, such as with cURL or Postman. If you do so with MATLAB using
webread
, use a separate MATLAB process from the one that
started the test client.
data = {struct('name', 'Ed', 'score', 83), struct('name', 'Toni', 'score', 91)}; body = mps.json.encoderequest(data); options = weboptions; % Create a weboptions object that instructs webread to return JSON text options.ContentType = 'text'; % Create a weboptions object that instructs webwrite to encode character vector data as JSON to post it to a web service options.MediaType = 'application/json'; response = webwrite('http://localhost:9910/studentapp/sortstudents', body, options); result = mps.json.decoderesponse(response);
Input Arguments
rhs
— Input arguments for deployed MATLAB function that is called
cell vector of any MATLAB data
type supported by MATLAB
Production Server
Input arguments for a MATLAB function deployed on MATLAB Production Server that is called, specified as a cell vector.
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: mps.json.encoderequest(rhs, 'Format',
'large')
Nargout
— Number of output arguments for function deployed on MATLAB Production Server
1 (default) | any positive integer
Number of output arguments for function deployed on MATLAB
Production Server, specified as comma-separated pair consisting of
'Nargout
' and number of output arguments.
mps.json.encoderequest(rhs, 'Nargout', 3)
.
Format
— Format to encode rhs
'small' (default) | 'large'
Format to encode rhs
, specified as comma-separated
pair consisting of 'Format
' and the format
'small
' or 'large
'.
The small
format is a simpler representation of
MATLAB data types in JSON, whereas the large
format
is a more generic representation. For more information, see JSON Representation of MATLAB Data Types (MATLAB Production Server).
NaNInfType
— Format to encode NaN
, Inf
, -Inf
in rhs
'string' (default) | 'object'
Format to encode NaN
, Inf
,
-Inf
in rhs
, specified as
comma-separated pair consisting of 'NaNInfType
' and
JSON data types 'string
' and
'object
'.
OutputFormat
— Format for response from MATLAB function deployed on MATLAB Production Server
'small' (default) | 'large'
Format for response from MATLAB function deployed on MATLAB
Production Server, specified as comma-separated pair consisting of
'OutputFormat
' and the format
'small
' or 'large
'.
Output format is set using mps.json.encoderequest(rhs,
'OutputFormat', 'large')
.
OutputNanInfType
— Type for response from MATLAB function deployed on MATLAB
Production Server containing NaN
, Inf
,
-Inf
'string' (default) | 'object'
Type for response from MATLAB function deployed on MATLAB
Production Server containing NaN
, Inf
,
-Inf
, specified as comma-separated pair
consisting of 'OutputNaNInfType
' and JSON data type
'string
' and 'object
'.
NaN-type for output response is set using
mps.json.encoderequest(rhs, 'OutputNaNInfType',
'object')
.
PrettyPrint
— Format text
for readability
false (default) | true
Format text
for readability, specified as a
comma-separated pair consisting of 'PrettyPrint
' and
logical 'true
' or 'false
'. Syntax
is
mps.json.encoderequest(rhs,'PrettyPrint',true)
.
Output Arguments
text
— JSON text
character vector
JSON-formatted text for JSON schema for MATLAB Production Server, returned as a character vector.
Version History
Introduced in R2018a
See Also
mps.json.encode
| mps.json.decode
| mps.json.decoderesponse
Topics
- JSON Representation of MATLAB Data Types (MATLAB Production Server)
- Create Deployable Archive for MATLAB Production Server (MATLAB Production Server)
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)