Main Content

mps.json.encoderequest

Convert MATLAB data in a server request to JSON text using MATLAB Production Server JSON schema

Description

text = mps.json.encoderequest(rhs) 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 'Nargout' and 'OutputFormat', that are needed to make a call to MATLAB Production Server.

example

text = mps.json.encoderequest(rhs,Name,Value) specifies additional options with one or more name-value pair arguments for specific input cases.

example

Examples

collapse all

mps.json.encoderequest({[1 2 3 4]})
ans =
    '{"rhs":[[[1,2,3,4]]],"nargout":1,"outputFormat":{"mode":"small","nanType":"string"}}'
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"}}'

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"}}'

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

collapse all

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')

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 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).

Format to encode NaN, Inf, -Inf in rhs, specified as comma-separated pair consisting of 'NaNInfType' and JSON data types 'string' and 'object'.

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').

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').

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

collapse all

JSON-formatted text for JSON schema for MATLAB Production Server, returned as a character vector.

Version History

Introduced in R2018a