@@ -175,8 +175,7 @@ protected String generateJson(Set<ConfigurableApplicationContext> contexts) {
175
175
else {
176
176
result .append ("\" type\" : null,\n " );
177
177
}
178
- String resource = StringUtils .replace (bd .getResourceDescription (), "\\ " , "/" );
179
- result .append ("\" resource\" : \" " ).append (resource ).append ("\" ,\n " );
178
+ result .append ("\" resource\" : \" " ).append (getEscapedResourceDescription (bd )).append ("\" ,\n " );
180
179
result .append ("\" dependencies\" : [" );
181
180
String [] dependencies = bf .getDependenciesForBean (beanName );
182
181
if (dependencies .length > 0 ) {
@@ -201,7 +200,7 @@ protected String generateJson(Set<ConfigurableApplicationContext> contexts) {
201
200
}
202
201
203
202
/**
204
- * Determine whether the specified bean is eligible for inclusion in the
203
+ * Determine whether the specified bean is eligible for inclusion in the
205
204
* LiveBeansView JSON snapshot.
206
205
* @param beanName the name of the bean
207
206
* @param bd the corresponding bean definition
@@ -213,4 +212,31 @@ protected boolean isBeanEligible(String beanName, BeanDefinition bd, Configurabl
213
212
(!bd .isLazyInit () || bf .containsSingleton (beanName )));
214
213
}
215
214
215
+ /**
216
+ * Determine a resource description for the given bean definition and
217
+ * apply basic JSON escaping (backslashes, double quotes) to it.
218
+ * @param bd the bean definition to build the resource description for
219
+ * @return the JSON-escaped resource description
220
+ */
221
+ protected String getEscapedResourceDescription (BeanDefinition bd ) {
222
+ String resourceDescription = bd .getResourceDescription ();
223
+ if (resourceDescription == null ) {
224
+ return null ;
225
+ }
226
+ StringBuilder result = new StringBuilder (resourceDescription .length () + 16 );
227
+ for (int i = 0 ; i < resourceDescription .length (); i ++) {
228
+ char character = resourceDescription .charAt (i );
229
+ if (character == '\\' ) {
230
+ result .append ('/' );
231
+ }
232
+ else if (character == '"' ) {
233
+ result .append ("\\ " ).append ('"' );
234
+ }
235
+ else {
236
+ result .append (character );
237
+ }
238
+ }
239
+ return result .toString ();
240
+ }
241
+
216
242
}
0 commit comments