package io.irontest.core;

import org.apache.commons.text.StrLookup;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class MapValueLookup extends StrLookup<String> {
    private Map<String, String> map;
    private boolean escapeForJSON;
    private Set<String> unfoundKeys = new HashSet<>();

    public MapValueLookup(Map<String, String> map, boolean escapeForJSON) {
        this.map = map;
        this.escapeForJSON = escapeForJSON;
    }

    @Override
    public String lookup(String key) {
        key = key.trim();
        for (Map.Entry<String, String> entry: map.entrySet()) {
            if (key.equals(entry.getKey())) {
                String value = entry.getValue();
                if (escapeForJSON) {
                    value = value.replace("\"", "\\\"");   //  replace " with \" for the result to be used in JSON string
                }
                return value;
            }
        }
        unfoundKeys.add(key);
        return null;    //  returning null leaves the reference untouched (not replaced) in the template string
    }

    public Set<String> getUnfoundKeys() {
        return unfoundKeys;
    }
}

@@ -735,32 +735,109 @@
 escapeForJSON) %7B
+  //  replace %5C and %22 with %5C%5C and %5C%22 for the result to be used in JSON string
 %0A               
@@ -869,85 +869,77 @@
 e(%22%5C
-%22
+%5C
 %22, %22%5C%5C%5C
-%22
+%5C
 %22);
+%0A 
    
-//  replace %22 with %5C%22 for the result to be used in JSON string
+                value = value.replace(%22%5C%22%22, %22%5C%5C%5C%22%22);
 %0A   

fix json string property resolving issue