Skip to content
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

JavaScriptFragment.asJson() #5108

Merged
merged 8 commits into from
Jan 27, 2024
3 changes: 2 additions & 1 deletion api/src/org/labkey/api/assay/AssayProtocolSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import org.labkey.api.util.FileUtil;
import org.labkey.api.util.HtmlString;
import org.labkey.api.util.HtmlStringBuilder;
import org.labkey.api.util.JsonUtil;
import org.labkey.api.util.PageFlowUtil;
import org.labkey.api.util.Path;
import org.labkey.api.util.StringExpressionFactory;
Expand Down Expand Up @@ -913,7 +914,7 @@ public HtmlString getFormattedHtml(RenderContext ctx)

try
{
Map<String, String> decodedVals = new ObjectMapper().readValue(val.toString(), Map.class);
Map<String, String> decodedVals = JsonUtil.DEFAULT_MAPPER.readValue(val.toString(), Map.class);
HtmlStringBuilder sb = HtmlStringBuilder.of(decodedVals.remove(ParticipantVisitResolverType.Serializer.STRING_VALUE_PROPERTY_NAME));

// Issue 21126 If lookup was pasted tsv, could still get a default list entry in properties list. Fix the redisplay
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jetbrains.annotations.NotNull;
import org.labkey.api.util.HtmlString;
import org.labkey.api.util.JsonUtil;

import java.io.IOException;

Expand Down Expand Up @@ -51,7 +52,7 @@ public HtmlString getFormattedHtml(RenderContext ctx)
if (null == value)
return HtmlString.EMPTY_STRING;

ObjectMapper mapper = new ObjectMapper();
ObjectMapper mapper = JsonUtil.createDefaultMapper();
DefaultPrettyPrinter pp = new DefaultPrettyPrinter();
pp.indentArraysWith(new DefaultIndenter());

Expand Down
2 changes: 1 addition & 1 deletion api/src/org/labkey/api/data/JsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void jacksonTest()
{
try
{
ObjectMapper mapper = new ObjectMapper().setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
ObjectMapper mapper = JsonUtil.createDefaultMapper().setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
Customer roundTripC1 = mapper.readValue(mapper.writeValueAsString(c1), Customer.class);
assertEquals(c1, roundTripC1);
Customer roundTripC2 = mapper.readValue(mapper.writeValueAsString(c2), Customer.class);
Expand Down
6 changes: 3 additions & 3 deletions api/src/org/labkey/api/exp/TemplateInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.labkey.api.data.JdbcType;
import org.labkey.api.util.JsonUtil;

import java.io.IOException;
import java.io.StringWriter;
Expand Down Expand Up @@ -73,7 +74,7 @@ public String toJSON()
{
StringWriter sw = new StringWriter();
JsonGenerator jsonGen = new JsonFactory().createGenerator(sw);
jsonGen.setCodec(new ObjectMapper());
jsonGen.setCodec(JsonUtil.createDefaultMapper());
jsonGen.writeObject(this);
return sw.toString();
}
Expand All @@ -90,8 +91,7 @@ public static TemplateInfo fromJson(String json)

try
{
ObjectMapper om = new ObjectMapper();
HashMap map = om.readValue(json, HashMap.class);
HashMap map = JsonUtil.DEFAULT_MAPPER.readValue(json, HashMap.class);
Double createdModuleVersion = null;
if (null != map.get("createdModuleVersion"))
createdModuleVersion = (Double) JdbcType.DOUBLE.convert(map.get("createdModuleVersion"));
Expand Down
4 changes: 2 additions & 2 deletions api/src/org/labkey/api/exp/api/DomainKindDesign.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import org.labkey.api.gwt.client.model.GWTDomain;
import org.labkey.api.util.JsonUtil;

import java.util.Map;

Expand All @@ -23,8 +24,7 @@ public void setDomainDesign(GWTDomain domainDesign)

public Map<String, Object> getOptions()
{
ObjectMapper mapper = new ObjectMapper();
return mapper.convertValue(_options, Map.class);
return JsonUtil.DEFAULT_MAPPER.convertValue(_options, Map.class);
}

public void setOptions(T options)
Expand Down
4 changes: 2 additions & 2 deletions api/src/org/labkey/api/exp/api/ExperimentJSONConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.labkey.api.query.ValidationException;
import org.labkey.api.security.User;
import org.labkey.api.security.permissions.ReadPermission;
import org.labkey.api.util.JsonUtil;
import org.labkey.api.util.Pair;
import org.labkey.api.util.URIUtil;

Expand Down Expand Up @@ -895,8 +896,7 @@ public static String getAliasJson(Map<String, String> importAliases, String curr

try
{
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(aliases);
return JsonUtil.DEFAULT_MAPPER.writeValueAsString(aliases);
}
catch (JsonProcessingException e)
{
Expand Down
6 changes: 3 additions & 3 deletions api/src/org/labkey/api/exp/property/DomainUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.labkey.api.util.DateUtil;
import org.labkey.api.util.GUID;
import org.labkey.api.util.JdbcUtil;
import org.labkey.api.util.JsonUtil;
import org.labkey.api.util.Pair;
import org.labkey.api.util.StringExpression;
import org.labkey.api.view.UnauthorizedException;
Expand Down Expand Up @@ -132,7 +133,7 @@ else if (AbstractAssayProvider.PARTICIPANT_VISIT_RESOLVER_PROPERTY_NAME.equalsIg
// addition nested properties containing ThawList list settings.
try
{
Map<String, String> decodedVals = new ObjectMapper().readValue(defaultValue.toString(), Map.class);
Map<String, String> decodedVals = JsonUtil.DEFAULT_MAPPER.readValue(defaultValue.toString(), Map.class);
String stringValue = decodedVals.get("stringValue");
if (stringValue != null)
return stringValue;
Expand Down Expand Up @@ -640,10 +641,9 @@ public static Domain createDomain(
{
throw new ValidationException(ve);
}
ObjectMapper mapper = new ObjectMapper();

arguments = kind.processArguments(container, user, arguments);
Object options = mapper.convertValue(arguments, kind.getTypeClass());
Object options = JsonUtil.DEFAULT_MAPPER.convertValue(arguments, kind.getTypeClass());
Domain created = kind.createDomain(domain, options, container, user, templateInfo);

if (created == null)
Expand Down
2 changes: 1 addition & 1 deletion api/src/org/labkey/api/files/view/filesWebPart.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<div id="<%=h(bean.getContentId())%>"></div>
<script type="text/javascript" nonce="<%=getScriptNonce()%>">
Ext4.onReady(function() {
var buttonActions = [<% String sep = ""; for(FilesWebPart.FilesForm.actions action : bean.getButtonConfig()) {%><%=text(sep)%>'<%=text(action.name())%>'<% sep = ","; }%>];
var buttonActions = [<% String sep = ""; for(FilesWebPart.FilesForm.actions action : bean.getButtonConfig()) {%><%=text(sep)%><%=q(action.name())%><% sep = ","; }%>];
var startDirectory;

<% if (bean.getDirectory() != null) { %>
Expand Down
6 changes: 3 additions & 3 deletions api/src/org/labkey/api/jsp/JspBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,12 @@ public static JavaScriptFragment json(JSONObject jsonObject, int indentFactor)
*/
final protected JavaScriptFragment q(String str)
{
return null == str ? JavaScriptFragment.NULL : JavaScriptFragment.unsafe(PageFlowUtil.jsString(str));
return JavaScriptFragment.asString(str);
}

final protected JavaScriptFragment q(HtmlString hs)
{
return null == hs ? JavaScriptFragment.NULL : JavaScriptFragment.unsafe(PageFlowUtil.jsString(hs.toString()));
return null == hs ? JavaScriptFragment.NULL : JavaScriptFragment.asString(hs.toString());
}

/**
Expand All @@ -282,7 +282,7 @@ final protected JavaScriptFragment q(@Nullable URLHelper url)

final protected JavaScriptFragment q(SafeToRender str)
{
return null == str ? JavaScriptFragment.NULL : JavaScriptFragment.unsafe(PageFlowUtil.jsString(str.toString()));
return null == str ? JavaScriptFragment.NULL : JavaScriptFragment.asString(str.toString());
}

protected HtmlString hq(String str)
Expand Down
5 changes: 3 additions & 2 deletions api/src/org/labkey/api/query/import.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<%@ page import="org.labkey.api.view.HttpView" %>
<%@ page import="org.labkey.api.view.template.ClientDependencies" %>
<%@ page import="static org.labkey.api.util.HtmlString.NDASH" %>
<%@ page import="org.labkey.api.util.JavaScriptFragment" %>
<%@ page extends="org.labkey.api.jsp.JspBase"%>
<%!
@Override
Expand Down Expand Up @@ -482,7 +483,7 @@
timeout: Ext4.Ajax.timeout,

items: getImportOptions(1).concat([
<%=unsafe(extraFormFields)%>
<%=JavaScriptFragment.unsafe(extraFormFields)%>
{
xtype: 'hidden', name: 'X-LABKEY-CSRF', value: LABKEY.CSRF
},
Expand Down Expand Up @@ -557,7 +558,7 @@
timeout: Ext4.Ajax.timeout,

items: getImportOptions(0).concat([
<%=unsafe(extraFormFields)%>
<%=JavaScriptFragment.unsafe(extraFormFields)%>
{
xtype: 'hidden', name: 'X-LABKEY-CSRF', value: LABKEY.CSRF
},
Expand Down
10 changes: 4 additions & 6 deletions api/src/org/labkey/api/reader/JSONDataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,8 @@ private JSONDataLoader(JsonParser parser)

protected void init(InputStream is) throws IOException
{
// TODO: JsonFactory is threadsafe and can be configured once and shared across the application.
JsonFactory factory = new JsonFactory();
_mapper = new ObjectMapper(factory);
_parser = factory.createParser(is);
_mapper = JsonUtil.createDefaultMapper();
_parser = _mapper.getFactory().createParser(is);
enterTopLevel();
}

Expand Down Expand Up @@ -936,8 +934,8 @@ public abstract static class JsonBaseTest extends Assert
{
JsonParser createParser(String json) throws IOException
{
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = new JsonFactory(mapper);
ObjectMapper mapper = JsonUtil.createDefaultMapper();
JsonFactory factory = mapper.getFactory();
JsonParser parser = factory.createParser(json);
parser.nextToken();
return parser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<%@ page import="java.util.ListIterator" %>
<%@ page import="java.util.Map" %>
<%@ page import="org.labkey.api.reports.report.python.IpynbReport" %>
<%@ page import="org.labkey.api.util.JsonUtil" %>
<%@ page import="org.labkey.api.util.JavaScriptFragment" %>
<%@ page extends="org.labkey.api.jsp.JspBase" %>
<%@ taglib prefix="labkey" uri="http://www.labkey.org/taglib" %>
<%!
Expand Down Expand Up @@ -198,8 +198,8 @@
preferSourceTab : <%=mode.preferSourceTab()%>,
sourceAndHelp : <%=sourceAndHelp%>,
redirectUrl : <%=q(bean.getRedirectUrl())%>,
sharedScripts : <%=unsafe(JsonUtil.DEFAULT_MAPPER.writeValueAsString(sharedScripts))%>,
reportConfig : <%=unsafe(JsonUtil.DEFAULT_MAPPER.writeValueAsString(reportConfig))%>,
sharedScripts : <%=JavaScriptFragment.asJson(sharedScripts)%>,
reportConfig : <%=JavaScriptFragment.asJson(reportConfig)%>,
script : <%=q(StringUtils.trimToEmpty(bean.getScript()))%>,
htmlEncodedProps: true
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
%>
<%@ page import="org.labkey.api.reports.report.JavaScriptReport.JavaScriptReportBean" %>
<%@ page import="org.labkey.api.util.UniqueID" %>
<%@ page import="org.labkey.api.util.JavaScriptFragment" %>
<%@ page extends="org.labkey.api.jsp.JspBase" %>
<%
JavaScriptReportBean bean = (JavaScriptReportBean)getModelBean();
Expand All @@ -28,7 +29,7 @@
(function()
{
// ========== Begin report writer's script ==========
<%=text(bean.script)%>
<%=JavaScriptFragment.unsafe(bean.script)%>
// ========== End report writer's script ==========
if (render && (typeof render === 'function'))
{
Expand Down Expand Up @@ -68,15 +69,15 @@
}

%>
render(getDataConfig, document.getElementById("<%=text(uniqueDivName)%>"));
render(getDataConfig, document.getElementById(<%=q(uniqueDivName)%>));
<%
}
else
{
%>
render({
<%=text(bean.model.getStandardJavaScriptParameters(16, false))%>
}, document.getElementById("<%=text(uniqueDivName)%>"));
<%=JavaScriptFragment.unsafe(bean.model.getStandardJavaScriptParameters(16, false))%>
}, document.getElementById(<%=q(uniqueDivName)%>));
<%
}
%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.labkey.api.exp.api.ExpMaterial;
import org.labkey.api.exp.api.ExpRun;
import org.labkey.api.security.User;
import org.labkey.api.util.JsonUtil;
import org.labkey.api.util.UnexpectedException;
import org.labkey.api.view.InsertView;

Expand Down Expand Up @@ -79,7 +80,7 @@ public static void decode(String stringValue, Map<String, Object> formDefaults,
{
try
{
Map<String, String> decodedVals = new ObjectMapper().readValue(stringValue, Map.class);
Map<String, String> decodedVals = JsonUtil.DEFAULT_MAPPER.readValue(stringValue, Map.class);
formDefaults.put(propName, decodedVals.remove(STRING_VALUE_PROPERTY_NAME));
formDefaults.putAll(decodedVals);
}
Expand All @@ -106,7 +107,7 @@ public static String encode(String resolverType, HttpServletRequest request)
try
{
// Prevent double encoding; if the resolverType string is already a json map, return it.
new ObjectMapper().readValue(resolverType, Map.class);
JsonUtil.DEFAULT_MAPPER.readValue(resolverType, Map.class);
return resolverType;
}
catch (IOException e)
Expand Down Expand Up @@ -140,7 +141,7 @@ public static String encode(String resolverType, HttpServletRequest request)

try
{
return new ObjectMapper().writeValueAsString(jsonValues);
return JsonUtil.DEFAULT_MAPPER.writeValueAsString(jsonValues);
}
catch (JsonProcessingException e)
{
Expand Down
12 changes: 6 additions & 6 deletions api/src/org/labkey/api/study/assay/thawListSelector.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
var sqvModel = Ext4.create('LABKEY.sqv.Model', {});

var sourceContainerCombo = Ext4.create('Ext.form.field.ComboBox', sqvModel.makeContainerComboConfig({
name: '<%= text(ThawListResolverType.THAW_LIST_LIST_CONTAINER_INPUT_NAME) %>',
name: <%= q(ThawListResolverType.THAW_LIST_LIST_CONTAINER_INPUT_NAME) %>,
id : 'thawListContainer',
fieldLabel: 'Folder',
value: <%=(container == null ? JavaScriptFragment.NULL : q(container.getPath())) %>,
Expand All @@ -97,7 +97,7 @@
forceSelection : true,
initialValue : <%=q(ctx.getForm().get(ThawListResolverType.THAW_LIST_LIST_SCHEMA_NAME_INPUT_NAME))%>,
fieldLabel : 'Schema',
name: '<%= text(ThawListResolverType.THAW_LIST_LIST_SCHEMA_NAME_INPUT_NAME) %>',
name: <%= q(ThawListResolverType.THAW_LIST_LIST_SCHEMA_NAME_INPUT_NAME) %>,
validateOnBlur: false,
width: 500
}));
Expand All @@ -109,7 +109,7 @@
typeAhead : true,
typeAheadDelay : 250,
fieldLabel : 'Query',
name: '<%= text(ThawListResolverType.THAW_LIST_LIST_QUERY_NAME_INPUT_NAME) %>',
name: <%= q(ThawListResolverType.THAW_LIST_LIST_QUERY_NAME_INPUT_NAME) %>,
initialValue : <%=q(ctx.getForm().get(ThawListResolverType.THAW_LIST_LIST_QUERY_NAME_INPUT_NAME))%>,
width: 500
}));
Expand All @@ -129,7 +129,7 @@
addParticipantVisitResolverSelectionChangeListener(function()
{
handleAllowedDefaultOptionsForThawList();
if (document.getElementById('<%=text(listTypeId)%>').checked)
if (document.getElementById(<%=q(listTypeId)%>).checked)
showChooseList();
else
thawListQueryPickerPanel.doLayout();
Expand All @@ -147,7 +147,7 @@

if (document.getElementById('RadioBtn-Lookup').checked)
{
var textRadio = Ext4.get('<%=text(textTypeId)%>');
var textRadio = Ext4.get(<%=q(textTypeId)%>);
if ((textRadio && textRadio.dom.checked) || <%=textType%>)
toggleDisableResetDefault(true); // Don't allow trying to set the default to the text type, as this is not supported.
}
Expand All @@ -172,7 +172,7 @@
{ %>
// Allow tabs in the TSV text area
<labkey:loadClientDependencies>
Ext.EventManager.on('<%= text(ThawListResolverType.THAW_LIST_TEXT_AREA_INPUT_NAME) %>', 'keydown', LABKEY.ext.Utils.handleTabsInTextArea);
Ext.EventManager.on(<%= q(ThawListResolverType.THAW_LIST_TEXT_AREA_INPUT_NAME) %>, 'keydown', LABKEY.ext.Utils.handleTabsInTextArea);
</labkey:loadClientDependencies>
<% } %>
</script>
Expand Down
Loading